I am working on lpc2468 ARM-based microcontroller. I am not able to configure the UART1 of the controller. Previously I was using UART0 for the same purpose, but due to some changes in hardware now I have to use UART1.
I want to connect GPRS module (SIMCOM 300) to the UART1 of the microcontroller. Here's my code so far:
void init_serial_gprs (void){
PINSEL7 |= 0x0000000F;
U1LCR = 0x00000080;
U1DLL = 0x00000041;
U1DLM = 0x00000000;
U1LCR = 0x03;
VICVectAddr7 = (unsigned)serial1_isr;
VICIntEnable = 1 << 7;
SBUF= U1RBR;
U1IER = 3;
}
void serial1_isr(void) __irq {
volatile char dummy;
volatile UWORD32 IIR1;
/*------------------------------------------------
Repeat while there is at least one interrupt source.
------------------------------------------------*/
while (((IIR1 = U1IIR) & 0x01) == 0)
{
switch (IIR1 & 0x0E)
{
case 0x06: /* Receive Line Status */
dummy = U1LSR; /* Just clear the interrupt source */
break;
case 0x04: /* Receive Data Available */
case 0x0C: /* Character Time-Out */
SBUF= U1RBR;
if(genflag.fl_m_psw || new_st == 0 ||new_st == 0x1f){
if(genflag.gprs_con) receive_data();
else
receive_data_gprs();
}
break;
case 0x02: /* THRE Interrupt */
if(genflag.gprs_con) transfer_data();
else transfer_data_gprs();
dummy = U1LSR;
break;
default:
break;
}
}
VICVectAddr = 0; /* Acknowledge Interrupt */
}
did you try polling the serial port first against a dumb terminal (hyperterm, minicom, putty) to make sure you have the basics then move on to interrupts?
Related
I'm trying to use the SAML21G18B MCU and the Mattairtech Arduino distribution, I need to configure TC0 to generate interrupts. Unfortunatly none of the existing SAMD timer libraries work with this MCU/distribution so I've been trying to configure and use TC0 from scratch ...
Mattairtech core on Github
I've taken the analogWrite() code from the Mattairtech core and hacked it into what I hope is something that configures TC0 for normal PWM - the same as when its used for analogWrite().
It compiles (YAY!) but I attempted to read from the count register to see if it is counting, and I get nothing. I also have no idea how to configure it to generate an interrupt on a match with the CC0 register.
All my code is below, any help, pointers, tips to get the interrupts working would be appreciated.
void TC0_Handler() {
//interrupt handler ...
}
static void SYNC_TC(Tc* TCx){
while(TCx->COUNT16.SYNCBUSY.reg & (TC_SYNCBUSY_SWRST | TC_SYNCBUSY_ENABLE | TC_SYNCBUSY_CTRLB | TC_SYNCBUSY_STATUS | TC_SYNCBUSY_COUNT));
}
//initialise TCO
void initTc0(bool sixteenBit, uint16_t value){
uint8_t timerCh = 0;
Tc* TCx = (Tc*)TC0;
//GCLK setup
GCLK->PCHCTRL[GCM_TC0_TC1].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0);
while ( (GCLK->PCHCTRL[GCM_TC0_TC1].reg & GCLK_PCHCTRL_CHEN) == 0 ); // wait for sync
//Disable TC
TCx->COUNT16.CTRLA.bit.ENABLE = 0;
SYNC_TC(TCx);
// Set Timer counter Mode to 16 bits, normal PWM
if(sixteenBit) TCx->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
else{
// Set Timer counter Mode to 8 bits, normal PWM
TCx->COUNT8.CTRLA.reg |= TC_CTRLA_MODE_COUNT8;
SYNC_TC(TCx);
// Set PER to maximum counter value
TCx->COUNT8.PER.reg = 0xFF;
}
SYNC_TC(TCx);
// Set TCx as normal PWM
TCx->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_NPWM;
SYNC_TC(TCx);
// Set the initial value for CC
if(sixteenBit) TCx->COUNT16.CC[timerCh].reg = (uint16_t) value;
else TCx->COUNT8.CC[timerCh].reg = (uint8_t) value;
SYNC_TC(TCx);
// Enable TCx
TCx->COUNT16.CTRLA.bit.ENABLE = 1;
SYNC_TC(TCx);
}
//Set the CCBuf register
void setTc(bool sixteenBit, uint16_t value){
uint8_t timerCh = 0;
Tc* TCx = (Tc*)TC0;
if(sixteenBit) TCx->COUNT16.CCBUF[timerCh].reg = (uint16_t)value;
else TCx->COUNT8.CCBUF[timerCh].reg = (uint8_t)value;
SYNC_TC(TCx);
}
uint16_t getCount(){
Tc* TCx = (Tc*)TC0;
TCx->COUNT16.CTRLBSET.bit.CMD = TC_CTRLBSET_CMD_READSYNC;
return TCx->COUNT16.COUNT.reg;
}
void setup() {
// put your setup code here, to run once:
initTc0(1, 0);
setTc(1, 500);
Serial.begin(1000000);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(getCount());
delay(100);
}
Ok, I answered my own question by hacking the tone() function. It uses TC1 to generate periodic interrupts that toggle a pin on and off to generate the tone. The following code lets you configure the timer with a clock prescaler and a value for CC - the timer counts up and when it matches CC the interrupt is called and it starts over.
This works for TC0, and is easily edited for TC1
#include "variant.h"
#include "sam.h"
//***************Use this for TC0
#define TCTimer TC0
#define timerIRQ TC0_IRQn
//Timer handler function if using TC0 - put whatever ISR code you want in here.
void TC0_Handler (void){
//clear the interrupt flag
TCTimer->COUNT16.INTFLAG.bit.MC0 = 1;
isrCounts++;
}
//***************Use this for TC1
/*
#define TCTimer TC1
#define timerIRQ TC1_IRQn
//Timer handler function if using TC1 - put whatever ISR code you want in here.
void TC1_Handler (void){
//clear the interrupt flag
TCTimer->COUNT16.INTFLAG.bit.MC0 = 1;
isrCounts++;
}*/
//Wait for registers to synchronise
#define WAIT_TC16_REGS_SYNC(x) while(x->COUNT16.SYNCBUSY.reg);
#define TONE_TC_TOP 0xFFFF
#define TONE_TC_CHANNEL 0
//Reset the timer hardware
static inline void resetTC (Tc* TCx){
// Disable TCx
TCx->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
WAIT_TC16_REGS_SYNC(TCx)
// Reset TCx
TCx->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
WAIT_TC16_REGS_SYNC(TCx)
while (TCx->COUNT16.CTRLA.bit.SWRST);
}
//setup the timer with a prescaler clock value, and the CC value.
//The timer incriments until it hits CC, then fires an interrupt and starts over
//With prescaler at 1 and CC at 65535 it interrupts 733 times per second
//Reduce CC to get a faster interrupt rate
//Prescaler must only be 1,2,4,8,16,64,25 or 1024
void setupTCTimer(uint32_t prescaler, uint32_t ccVal ){
NVIC_DisableIRQ(timerIRQ);
NVIC_ClearPendingIRQ(timerIRQ);
NVIC_SetPriority(timerIRQ, 0);
// Enable GCLK for timer used
GCLK->PCHCTRL[GCM_TC0_TC1].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0);
while ( (GCLK->PCHCTRL[GCM_TC0_TC1].reg & GCLK_PCHCTRL_CHEN) == 0 ); // wait for sync
uint32_t prescalerConfigBits;
uint32_t ccValue;
ccValue = ccVal;//toneMaxFrequency / frequency - 1;
prescalerConfigBits = TC_CTRLA_PRESCALER_DIV1;
switch(prescaler){
case 0: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV1; break;
case 1: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV1; break;
case 2: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV2; break;
case 4: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV4; break;
case 8: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV8; break;
case 16: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV16; break;
case 64: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV64; break;
case 256: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV256; break;
case 1024: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV1024; break;
default: prescalerConfigBits = TC_CTRLA_PRESCALER_DIV1; break;
}
resetTC(TCTimer);
uint16_t tmpReg = 0;
tmpReg |= TC_CTRLA_MODE_COUNT16; // Set Timer counter Mode to 16 bits
tmpReg |= prescalerConfigBits;
TCTimer->COUNT16.CTRLA.reg |= tmpReg;
WAIT_TC16_REGS_SYNC(TCTimer)
TCTimer->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
WAIT_TC16_REGS_SYNC(TCTimer)
TCTimer->COUNT16.CC[TONE_TC_CHANNEL].reg = (uint16_t) ccValue;
WAIT_TC16_REGS_SYNC(TCTimer)
// Enable the TCTimer interrupt request
TCTimer->COUNT16.INTENSET.bit.MC0 = 1;
}
//Start the timer
//you need to call setupTCTimer with prescaler and CC values before you start the timer
void startTCTimer(){
// Enable TCTimer
TCTimer->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
WAIT_TC16_REGS_SYNC(TCTimer)
NVIC_EnableIRQ(timerIRQ);
timerActive = true;
}
//stop the timer
//This resets it so you need to call setupTCTimer with prescaler and CC values before restarting
void stopTCTimer(){
resetTC(TCTimer);
timerActive = false;
}
I'm attempting to get a smartphone app to communicate with an MCU (STM32F030R8). The step I'm currently on is getting the WiFi module to talk with the MCU. I'm able use the AT firmware to configure the ESP when using an Arduino Uno/PC terminal, but want to essentially do the same via the STM board.
When I load the code to the STM and use Tera Term to send data to the STM it should be able to send AT commands to the ESP, but it isn't working. Right now I'm at a loss as to why this isn't working. I'm looking for pointers on the code that I've probably missed or other principles I've overlooked/am unaware of.
I've searched around on the internet the past couple days and haven't come up with much that helps. This is effectively the sum of my experience with C. Thanks for your help!
The hardware is set up as follows...
STM32 ESP
PA10 TX
PA9 RX
3.3V VCC
3.3V CH_PD
GND GND
UNO ESP
TX TX
RX RX
3.3V VCC
3.3V CH_PD
GND GND
The code I've frankenstein'd together is below. I'm using Keil uVision 5.
#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>
void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);
int main(void)
{
// disable the interrupt detection
__disable_irq();
RCC->AHBENR |= 0x00020000; // enable port A clock
GPIOA->MODER |= 0x00000400; // set mode for PA5
USART1_Init();
USART2_Init();
char strCommand[50];
NVIC_EnableIRQ(USART1_IRQn);
__enable_irq();
printf("\r\nInterface for ESP8266 AT commands.\r\n");
printf("Please enter the AT command: ");
gets(strCommand);
USART1_Write(strCommand);
printf("\r\n");
while (1);
}
// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
// wait while the tx buffer is empty
while (!(USART1->ISR & 0x00000080));
for (int i = 0; i < strlen(ch); i++)
{
USART1->TDR = (ch[i] & 0xFF);
}
USART1->TDR = 0x0000000D;
USART1->TDR = 0x0000000A;
}
// read data from the peripheral via USART1
char USART1_Read(void)
{
// wait while the rx buffer is empty
while (USART1->ISR & 0x00000020);
return USART1->RDR;
}
// write data to the PC via USART2
int USART2_Write(int ch)
{
// wait while the tx buffer is empty
while (!(USART2->ISR & 0x00000080));
USART2->TDR = (ch & 0xFF);
return ch;
}
// read data from the PC via USART2
int USART2_Read(void)
{
// wait while the rx buffer is empty
while (!(USART2->ISR & 0x00000020));
return USART2->RDR;
}
// initialize USART1
void USART1_Init(void)
{
RCC->APB2ENR |= 0x00004000; // enable USART1 clock
GPIOA->AFR[1] |= 0x00000110; // set AF1 to PA9, PA10
GPIOA->MODER |= 0x00280000; // define GPIO modes to alternate function for PA9, PA10
USART1->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART1->CR1 = 0x0000002C; // enable te, re, and rx interrupt
USART1->CR1 |= 0x00000001; // enable ue
}
// initialize USART2
void USART2_Init(void)
{
RCC->APB1ENR |= 0x00020000; // enable USART2 clock
GPIOA->AFR[0] |= 0x00001100; // set AF1 to PA2, PA3
GPIOA->MODER |= 0x000000A0; // define GPIO modes to alternate function for PA2, PA3
USART2->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART2->CR1 = 0x0000000C; // enable te and re
USART2->CR1 |= 0x00000001; // enable ue
}
// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
char c;
if (USART1->ISR & 0x00000020)
{
c = USART1->RDR;
//GPIOA->ODR ^= 0x00000020; // toggle the PA5 state
printf("%c", c);
}
}
// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};
int fgetc(FILE *f)
{
int c;
c = USART2_Read();
if (c == '\r')
{
USART2_Write(c);
c = '\n';
}
USART2_Write(c);
return c;
}
int fputc(int c, FILE *f)
{
return USART2_Write(c);
}
EDIT:
I've modified the code a bit and kept trying. When attempting to change the broadcasted network name/password (it didn't work) it now replies with the first two typed characters in the command consistently and after a moment put forth the gibberish at looks like it should have been summoned with an "AT+GMR" command. The following code is what was uploaded to the device. Any thoughts on how this could have happened? Thanks!
#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>
void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);
int main(void)
{
// disable the interrupt detection
__disable_irq();
RCC->AHBENR |= 0x00020000; // enable port A clock
GPIOA->MODER |= 0x00000400; // set mode for PA5
USART1_Init();
USART2_Init();
char strCommand[50];
NVIC_EnableIRQ(USART1_IRQn);
__enable_irq();
printf("\r\nInterface for ESP8266 AT commands.\r\n");
printf("Please enter the AT command: ");
gets(strCommand);
USART1_Write(strCommand);
printf("\r\n");
while (1);
}
// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
// wait for the tx buffer to be empty
while (!(USART1->ISR & 0x00000080));
for (int i = 0; i < strlen(ch); i++)
{
USART1->TDR = (ch[i] & 0xFF);
}
// USART1->TDR = '\r';
// USART1->TDR = '\n';
}
// read data from the peripheral via USART1
char USART1_Read(void)
{
// wait while the rx buffer is empty
while (USART1->ISR & 0x00000020);
return USART1->RDR;
}
// write data to the PC via USART2
int USART2_Write(int ch)
{
// wait while the tx buffer is empty
while (!(USART2->ISR & 0x00000080));
USART2->TDR = (ch & 0xFF);
return ch;
}
// read data from the PC via USART2
int USART2_Read(void)
{
// wait while the rx buffer is empty
while (!(USART2->ISR & 0x00000020));
return USART2->RDR;
}
// initialize USART1
void USART1_Init(void)
{
RCC->APB2ENR |= 0x00004000; // enable USART1 clock
GPIOA->AFR[1] |= 0x00000110; // set AF1 to PA9, PA10
GPIOA->MODER |= 0x00280000; // define GPIO modes to alternate function for PA9, PA10
USART1->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART1->CR1 = 0x0000002C; // enable te, re, and rx interrupt
USART1->CR1 |= 0x00000001; // enable ue
}
// initialize USART2
void USART2_Init(void)
{
RCC->APB1ENR |= 0x00020000; // enable USART2 clock
GPIOA->AFR[0] |= 0x00001100; // set AF1 to PA2, PA3
GPIOA->MODER |= 0x000000A0; // define GPIO modes to alternate function for PA2, PA3
USART2->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART2->CR1 = 0x0000000C; // enable te and re
USART2->CR1 |= 0x00000001; // enable ue
}
// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
char c;
while (1)
{
if (USART1->ISR & 0x00000020)
{
c = USART1->RDR;
}
else
{
break;
}
printf("%c", c);
}
}
// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};
int fgetc(FILE *f)
{
int c;
c = USART2_Read();
if (c == '\r')
{
USART2_Write(c);
c = '\n';
}
USART2_Write(c);
return c;
}
int fputc(int c, FILE *f)
{
return USART2_Write(c);
}
I was able to get the ESP to respond to the AT commands with the following code. There are sections commented out as I was simply frankensteining my way to 'working' code. I was able to change the SSID and password via a hard coded string. I simply sent it by pressing 'Enter' to get the code to proceed.
As it turns out the issue laid in sending data to the ESP (rather than receiving, which was my initial theory). Now I need to get the code to receive an uint8_t[] from the terminal, but that is a separate issue from the original question so I'll mark this as answered.
Thank you #hamboy75, #Juraj, and #KamilCuk for your help!
#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>
void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);
void USART1_Get(unsigned char *string);
void USART1_PutChar(uint8_t ch);
void USART1_Write2(uint8_t *str);
int main(void)
{
// disable the interrupt detection
__disable_irq();
RCC->AHBENR |= 0x00020000; // enable port A clock
GPIOA->MODER |= 0x00000400; // set mode for PA5
USART1_Init();
USART2_Init();
char strCommand[50];
uint8_t str[] = "AT+CWSAP_CUR=\"tempNetwork\",\"tempPassword\",1,2,1,0\r\n";
NVIC_EnableIRQ(USART1_IRQn);
__enable_irq();
printf("\r\nInterface for ESP8266 AT commands.\r\n");
printf("Please enter the AT command: ");
gets(strCommand);
//USART1_Write(strCommand);
USART1_Write2(str);
printf("\r\n");
while (1);
}
// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
// wait for the tx buffer to be empty
while (!(USART1->ISR & 0x00000080));
for (int i = 0; i < strlen(ch); i++)
{
//USART1->TDR = (ch[i] & 0xFF);
}
//USART1->TDR = '\r';
//USART1->TDR = '\n';
USART1->TDR = ('T' & 0xFF);
USART1->TDR = ('T' & 0xFF);
USART1->TDR = '\r';
USART1->TDR = '\n';
}
// write a string via USART1
void USART1_Write2(uint8_t *str)
{
while (*str != 0)
{
USART1_PutChar(*str);
str++;
}
}
// write one character via USART1
void USART1_PutChar(uint8_t ch)
{
while (!(USART1->ISR & 0x00000080));
USART1->TDR = ch;
}
// read data from the peripheral via USART1
char USART1_Read(void)
{
// wait while the rx buffer is empty
while (USART1->ISR & 0x00000020);
return USART1->RDR;
}
// write data to the PC via USART2
int USART2_Write(int ch)
{
// wait while the tx buffer is empty
while (!(USART2->ISR & 0x00000080));
USART2->TDR = (ch & 0xFF);
return ch;
}
// read data from the PC via USART2
int USART2_Read(void)
{
// wait while the rx buffer is empty
while (!(USART2->ISR & 0x00000020));
return USART2->RDR;
}
// initialize USART1
void USART1_Init(void)
{
RCC->APB2ENR |= 0x00004000; // enable USART1 clock
GPIOA->AFR[1] |= 0x00000110; // set AF1 to PA9, PA10
GPIOA->MODER |= 0x00280000; // define GPIO modes to alternate function for PA9, PA10
USART1->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART1->CR1 = 0x0000002C; // enable te, re, and rx interrupt
USART1->CR1 |= 0x00000001; // enable ue
}
// initialize USART2
void USART2_Init(void)
{
RCC->APB1ENR |= 0x00020000; // enable USART2 clock
GPIOA->AFR[0] |= 0x00001100; // set AF1 to PA2, PA3
GPIOA->MODER |= 0x000000A0; // define GPIO modes to alternate function for PA2, PA3
USART2->BRR = 0x00000341; // set the baud rate, 9600 # 8MHz
USART2->CR1 = 0x0000000C; // enable te and re
USART2->CR1 |= 0x00000001; // enable ue
}
// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
char c;
while (1)
{
if (USART1->ISR & 0x00000020)
{
c = USART1->RDR;
printf("%c", c);
}
else
{
break;
}
}
}
// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};
int fgetc(FILE *f)
{
int c;
c = USART2_Read();
if (c == '\r')
{
USART2_Write(c);
c = '\n';
}
USART2_Write(c);
return c;
}
int fputc(int c, FILE *f)
{
return USART2_Write(c);
}
I'm able to receive with the following code, but unfortunately, nothing is sent back. What am I doing wrong?
#include <pic18f25k80.h>
#include "config.h"
#include <usart.h>
int i = 0;
unsigned char MessageBuffer[200];
void main() {
OSCCONbits.IRCF = 0b110; // 8MHz
TRISB6 = 0; // TX set as output
TRISB7 = 0; // RX set as output
// Clear TX interrupt
// Set RX interrupt
// 8-bit Asynch. mode
// BRGH = 1 = high baud mode
// 51 = ((8MHz/baud)/16)-1 with baud = 9600
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE
& USART_EIGHT_BIT & USART_BRGH_HIGH, 51 );
RC2IF = 0; // reset RX2 flag
RC2IP = 0; // not high priority
RC2IE = 1; // Eneble RX2 interrupt
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts
RCSTA2bits.SPEN = 1; // enable USART
while(1){
}
}
void interrupt ISR () {
if(PIR3bits.RC2IF == 1) {
if(i<200) { // buffer size
MessageBuffer[i] = Read2USART(); // read byte from RX reg
if (MessageBuffer[i] == 0x0D) { // check for return key
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i=0;
return;
}
i++;
RC2IF = 0; // clear RX flag
} else {
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i = 0;
return;
}
}
}
I'm transmitting the 0x41 hex code, I checked with the scope and see that is is being received. And according to the code I have, an echo of the received data should be sent back. When I check the TX pin, nothing is happening.
Add USART_CONT_RX to Open2USART to enable continuous receive.
Also, it's a good idea to do the minimum necessary in the interrupt service routine. Consider something like:
void interrupt ISR () {
char data;
if(PIR3bits.RC2IF == 1) {
data = Read2USART(); // always read byte from RX reg (clears RC2IF)
if(i<200) { // buffer size
MessageBuffer[i] = data; // read byte from RX reg
i++;
}
else{
// flag buffer full error
}
}
}
and doing the rest of what you are doing in the while(1) loop.
I am running a C program on an AVR chip. Whenever a serial signal is heard, it runs the serial interrupt ISR (USART_RX_vect). In this method it should turn on change to = 1;. Then in my main while loop, it should clear the LCD and display it and then set change = 0 again.
This is to stop it continually doing the calulations, and displaying the result on the LCD a million times a minute..
However, when the interrupt method changes the change variable to 1, it does not seem to change it "globally" and in the main method it is always 0..
There is a bit of stuff in here that is for debugging purposes.
/* LCD DEFINES */
#define LED PB5
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
/* UART SERIAL DEFINES */
#define F_CPU 16000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
#define STARTCHAR 'R'
#define ENDCHAR 'E'
char reading;
char inputBuffer[12];
char readStatus;
uint8_t position;
int change;
char output;
int result;
struct Axis
{
uint8_t axisNumber;
uint16_t position;
uint16_t oldPosition;
} axis1, axis2, axis3;
/* SETUP UART */
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
UDR0 = data;
}
unsigned char USART_Receive( void )
{
return UDR0;
}
/*****************************************************************/
int main(void)
{
/* INITALISE SERIAL */
USART_Init(MYUBRR);
/* Turn on Receive Complete Interrupt */
UCSR0B |= (1 << RXCIE0);
/* Turn On GLobal Interrupts */
sei();
position = 0;
change = 0;
/* Initialise LCD */
lcd_init(LCD_DISP_ON); /* Initialize display, cursor off. */
lcd_clrscr();
lcd_puts("READY");
//Turn on LED 13
set_output(PORTB,LED);
output_low(PORTB,LED);
while (1) /* Loop forever */
{
if (change == 1)
{
//If not reading, display the result on the LCD display.
axis1.position = (inputBuffer[0]<< 8) | inputBuffer[1];
axis2.position = (inputBuffer[2]<< 8) | inputBuffer[3];
axis3.position = (inputBuffer[4]<< 8) | inputBuffer[5];
char axis1Printout[12];
char axis2Printout[12];
char axis3Printout[12];
sprintf(axis1Printout,"%u ", axis1.position);
sprintf(axis2Printout,"%u ", axis2.position);
sprintf(axis3Printout,"%u ", axis3.position);
char output[40] = "";
strcat(output, axis1Printout);
strcat(output, axis2Printout);
//strcat(output, axis3Printout);
lcd_clrscr(); /* Clear the screen*/
lcd_puts(output);
_delay_ms(300);
change = 0;
}
}
}
/* INTERRUPTS */
ISR (USART_RX_vect)
{
change = 1;
unsigned char input = USART_Receive();
if (input == 'R')
{
readStatus = 0; //Reading
position = 0;
}
else if ((input != 'E') && (position < 12) && (position > -1))
{
inputBuffer[position] = input;
position++;
}
else if (input == 'E')
{
readStatus = 1; //Stop Reading
position = -1;
output_high(PORTB,LED);
}
}
You need to declare change using the volatile keyword:
volatile int change;
This tells the two 'threads' (main execution loop and your ISR code) to not 'cache' the value in a register, but always retrieve it from memory.
Edit: There's another problem with the code - in your main loop, by the time you set changed to 0, you may have already had another interrupt which should have triggered your loop to run again. The easy-but-not-guaranteed fix is to immediately set changed to 0 straight after you check it. The proper way would be to use a lock - but depending on your situation, the first option might do.
Make the variable declaration volatile to ensure that a changed value is written imediately to the variable in memory.
An object shared by an interrupt handler and the application code should be qualified as volatile in the declaration.
Without the qualifier, the implementation can assume the object cannot change unexpectedly in the application code and can cache the variable (in a register for example) for optimizations while executing the application code.
My UART is configured for 4800baud rate, Well I am not able to RX the NMEA sentences(GPS eval board) using the UART code pasted below. Individually GPS+Terminal and UART+Terminal is working fine . But GPS+UART , I am never able to RX any sentences
GPS default buad rate is 4800
Any idea where I am going wrong with UART???
/**
* uart default at 4800 ACLK= 32.768KHz
* #4800 bps
*/
void UART_default(void){
P3SEL = BIT3+BIT4;
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_1;
UCA0BR0 = 0x06;
UCA0BR1 = 0x00;
UCA0MCTL |= UCBRS_6+UCBRF_0;
UCA0CTL1 &= ~UCSWRST;
UCA0IE |= UCRXIE;
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
volatile char byte;
switch(__even_in_range(UCA0IV,4))
{
case 0:break; // Vector 0 - no interrupt
case 2:
byte= UCA0RXBUF;
if (byte == '\r') {
push_char(byte); // At end of Data transmission
ptr = 0;
}
else{
push_char(byte);
ptr++;
}
break;
case 4:break;
default: break;
}
}
Did you try to replace the GPS by a PC with a USB <-> UART converter ?
This could validate the baudrate setup in your MSP430 as well as the ability to receive something.