Hi I write code below for initial External Interrupt for LPC2138 in KEIL 4.7 Compiler and when run code in proteus software , code dosent Work. I double check VIC and EXTINT registers seems correct. thanks
Project Picture on Proteus
one Switch on EXTINT2 (P0.15) and one LED on P1.25
#include <LPC213x.h>
void delay(int count);
void init_ext_interrupt(void);
__irq void Ext_ISR(void);
int main (void)
{
init_ext_interrupt(); // initialize the external interrupt
while (1)
{
}
}
void init_ext_interrupt(void) //Initialize Interrupt
{
EXTMODE = (1<<2); //Edge sensitive mode on EINT2
EXTPOLAR &= ~(1<<2); //Falling Edge Sensitive
PINSEL0 = 0x80000000; //Select Pin function P0.15 as EINT2
/* initialize the interrupt vector */
VICIntSelect &= ~(1<<16); // EINT2 selected as IRQ 16
VICVectAddr5 = (unsigned)Ext_ISR; // address of the ISR
VICVectCntl5 = (1<<5) | 16;
VICIntEnable = (1<<16); // EINT2 interrupt enabled
EXTINT &= ~(1<<2); //Set interrupt
}
__irq void Ext_ISR(void) // Interrupt Service Routine-ISR
{
IO1DIR |= (1<<25);
IO1SET |= (1<<25); // Turn ON LED
delay(100000);
IO1CLR |= (1<<25); // Turn OFF LED
EXTINT |= (1<<2); //clear interrupt
VICVectAddr = 0; // End of interrupt execution
}
void delay(int count)
{
int j=0,i=0;
for(j=0;j<count;j++)
{
for(i=0;i<35;i++);
}
}
You should correct the line:
(VICVectCntl5 = (1<<5) | 16;)
to:
(VICVectCntl5 = 0x20 | 16;)
as datasheet said.
Related
I am trying to use the SPI communication to read data from the ADXL345 accelerometer. I configured the different pins and SPI in master mode, and tried reading the x, y and z axis accelerations.
My issue is that the SPI readings are always 0. I tried debugging to find the issue and I realized that RXNE is never set even though I'm transmitting data and I don't really get why.
I'm using STM32F103 Board.
Here's my code:
#include "Driver_GPIO.h"
#include "stm32f10x.h"
uint8_t RxData[6];
int x,y,z;
float x_acc,y_acc,z_acc;
void GPIO_Config (void)
{
MyGPIO_Struct_TypeDef NSS={GPIOA,4,Out_OD}; // Output Open Drain
MyGPIO_Struct_TypeDef SCK={GPIOA,5,AltOut_Ppull}; // Alternate Output Push-Pull
MyGPIO_Struct_TypeDef MISO={GPIOA,6,In_Floating}; // Input Floating
MyGPIO_Struct_TypeDef MOSI={GPIOA,7,AltOut_Ppull}; // Alternate Output Push-Pull
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //enable GPIOA clk
MyGPIO_Init(&NSS);
MyGPIO_Init(&SCK);
MyGPIO_Init(&MISO);
MyGPIO_Init(&MOSI);
}
void SPI_Enable (void)
{
SPI1->CR1 |= (SPI_CR1_SPE); // SPE=1, Peripheral enabled
}
void SPI_Disable (void)
{
SPI1->CR1 &= ~(SPI_CR1_SPE); // SPE=0, Peripheral Disabled
}
void CS_Enable (void)
{
GPIOA->BSRR |= GPIO_BSRR_BR9;
}
void CS_Disable (void)
{
GPIOA->BSRR |= GPIO_BSRR_BS9;
}
void SPI_Config(void){
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // Enable SPI1 CLock
SPI1->CR1 |= SPI_CR1_CPOL| SPI_CR1_CPHA; // CPOL=1, CPHA=1
SPI1->CR1 |= SPI_CR1_MSTR; // Master Mode
SPI1->CR1 |= (SPI_CR1_BR_0)| (SPI_CR1_BR_1); // BR[2:0] = 400: fPCLK/16, PCLK2 = 72MHz, SPI clk = 3.375MHz
SPI1->CR1 &= ~SPI_CR1_LSBFIRST; // LSBFIRST = 0, MSB first
SPI1->CR1 |= (SPI_CR1_SSM) | (SPI_CR1_SSI); // SSM=1, SSI=1 -> Software Slave Management
SPI1->CR1 &= ~SPI_CR1_RXONLY; // RXONLY = 0, full-duplex
SPI1->CR1 &= ~SPI_CR1_DFF; // DFF=0, 8 bit data
SPI1->CR2 = 0;
}
void SPI_Transmission(uint8_t *data, int size){
uint8_t clear;
//check flag TxE //
int i=0;
while (i<size)
{
while (!((SPI1->SR)&(SPI_SR_TXE))){}; // buffer is empty
*(volatile uint8_t *)&SPI1->DR = data[i];
i++;
}
while (!((SPI1->SR)&(SPI_SR_TXE))){}; // buffer is empty
while (((SPI1->SR)&(SPI_SR_BSY))){}; // buffer not communicating
clear= SPI1->DR; // empty Overrun flag
clear= SPI1->SR;
}
void SPI_Receive (uint8_t *data,int size)
{
while (size)
{
while (((SPI1->SR)&(SPI_SR_BSY))) {}; // buffer not communicating
*(volatile uint8_t *)&SPI1->DR = 0; // dummy data
while (!((SPI1->SR) &(SPI_SR_RXNE))){};
// buffer is not empty
*data++= *(volatile uint8_t *)&SPI1->DR;
size--;
}
}
void adxl345_write (uint8_t address, uint8_t value)
{
uint8_t data[2];
data[0] = address|0x40; // multibyte write
data[1] = value;
CS_Enable (); // pull the cs pin low
SPI_Transmission (data,2); // write data to register
CS_Disable (); // pull the cs pin high
}
void adxl345_read (uint8_t address, uint8_t *RxData)
{
address |= 0x80; // read operation
address |= 0x40; // multibyte read
CS_Enable (); // pull the pin low
SPI_Transmission (&address,1); // send address
SPI_Receive (RxData,6); // receive 6 bytes data
CS_Disable ();; // pull the pin high
}
void adxl345_init (void)
{
adxl345_write (0x31, 0x01); // data_format range= +- 4g
adxl345_write (0x2d, 0x00); // reset all bits
adxl345_write (0x2d, 0x08); // power_cntl measure and wake up 8hz
}
int main(void)
{
GPIO_Config();
SPI_Config();
SPI_Enable();
adxl345_init();
do{
adxl345_read(0x32,RxData);
x = ((RxData[1]<<8)|RxData[0]); // DATA X0, X1
y = ((RxData[3]<<8)|RxData[2]); // DATA Y0, Y1
z = ((RxData[5]<<8)|RxData[4]); // DATA Z0, Z1
// Scale Factor for Xout, Yout and Zout is 7.8 mg/LSB for a +-4g, 10-bit resolution
// ==> multiply by 0.0078 to get real acceleration values
x_acc= x * 0.0078;
y_acc= y * 0.0078;
z_acc= z * 0.0078;
}while(1);
}
As already stated you have a lot of issues here
Why NSS pin is configured open-drain? Typically CS lines are push-pull. I don't know the schematics, but this is the first time I see an open-drain CS
In GPIO_Config NSS is pin 4, yet pin 9 is toggled from CS_Enable
If F1 series there is separate clocking bit for the alternate functions, it's not enabled
It is also weird that you are telling that RXNE is always zero and the readings returns zero. Your code should stuck in while loops if RXNE stays zero
I will not analyze magic numbers as I do not have time check every number in the RM. But you have many obvious issues.
Deley or readback is required after enabling the peripheral clock. You instantly set the registers which is wrong. Add __DSB(); or readback (for example (void)RCC->APB2ENR;). Same for all peripherals
I am working on a project and I have two external interrupts (push buttons) and using TIMER1 compare match mode, I want the timer to reach the top value twice (here I have it one time only because I was trying to see where's the problem) then turn off a dc motor but it doesn't seem to fire the interrupt, the external interrupts work btw, I even tried to toggle a led in the ISR and it won't do anything. Here's the code:
uint8 volatile flag=0; //for stopping the motor
void INT0_init(void)
{
MCUCR = MCUCR | (1<ISC01) ; /*interrupt with rising edge */
MCUCR = MCUCR | (1<<ISC00);
GICR |= (1<<INT0);
DDRD = DDRD & (~(1<<PD2)); /* setting PD2 as input pin */
}
ISR (INT0_vect)
{
SET_BIT(PORTB, PB3); //when the interrupt happens the motor moves in anti clockwise direction
CLEAR_BIT(PORTB, PB4);
TIMER1_ctcMode(0x0000, 0xFFFF);
while( PIND & (1<<PD2) ) {} /*to stay in this state as long as it got pressed once */
}
void INT1_init(void)
{
MCUCR = MCUCR | (1<ISC11) ; /*interrupt with rising edge */
MCUCR = MCUCR | (1<<ISC10);
GICR |= (1<<INT1);
DDRD = DDRD & (~(1<<PD3)); /* setting PD3 as input pin */
}
ISR (INT1_vect)
{
SET_BIT(PORTB, PB3); //when the interrupt happens the motor moves in clockwise directio
CLEAR_BIT(PORTB, PB4);
TIMER1_ctcMode(0x0000, 0xFFFF); //call the timer for 8-ish seconds
while( PIND & (1<<PD3) ) {} /*to stay in this state as long as it got pressed once */
}
ISR (TIMER1_COMPA_vect) //timer isr
{
flag++;
PORTA ^=(1<<0);
_delay_ms(500);
}
int main()
{
SET_BIT(SREG,7); /* setting the i-bit */
SET_BIT(DDRB, PB3); //setting the pins as output pins of the H-bridge
SET_BIT(DDRB, PB4);
TIMER1_initCtcMode();
INT0_init(); /* initializing interrupt0 */
INT1_init(); /*initializing interrupt1 */
while (1)
{
if(flag==1)
{
TIMER1_deinit(); // to stop the timer
CLEAR_BIT(PORTB, PB3); //when the timer finishes counting stop the motor
CLEAR_BIT(PORTB, PB4);
flag=0; //set the flag to zero for the next time the timer is used
}
}
}
and here's the timer configuration:
void TIMER1_initCtcMode(void)
{
SREG= SREG | (1<<7); //setting the i-bit
TIMSK |=(1<<OCIE1A); //setting interrupt enable for timer1
TIFR &=~(1<<OCF1A);
TCCR1A |=(1<<FOC1A) ;
TCCR1A |= (1<<FOC1B); //setting the non-pwm bits
TCCR1B |=(1<<WGM12);
TCCR1B |= (1<<CS12);
TCCR1B |= (1<<CS10); //setting prescalar at 1024
TCCR1B = TCCR1B &( ~(1<<CS11));
TCCR1A &= ~(1<<COM1A0); //clearing them for non-pwm mode
TCCR1A &= ~(1<<COM1A1);
}
void TIMER1_ctcMode(volatile uint16 initialValue, volatile uint16 topValue)
{
TCNT1=initialValue; //initial value
OCR1A=topValue; //top value to reach 8.3sec
}
void TIMER1_deinit(void)
{
CLEAR_BIT(TCCR1B, CS12);
CLEAR_BIT(TCCR1B, CS10);
CLEAR_BIT(TCCR1B, CS11);
TCNT1=0;
OCR1A=0;
TIMSK &=~(1<<OCIE1A);
}
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);
}
When the timer match MR0, run the interrupt service routine At the end of the interrupt service routine. I don't return from service routine to main program. Why does my program not return from service routine?
See answer below for complete code
the code is /* Timer.h */
#include "LPC214x.h"
#include "main.h"
#define VIC_EN 5
#define VIC_TIMER0 4
#define MR0 0
void timer_init(void);
void timer_isr(void);
/* Timer.c */
volatile uint8_t flag;
void timer_init()
{
//disable and reset timer counters
T0TCR = BV(1);
//use T0 as TIMER:
T0CTCR = 0x00;
//set prescalar
T0PR = 15000000-1;
//setup MR0 for 5 sec
T0MR0 = 4; //4+1
//enable intr on MR0, reset
T0MCR |= BV(0) | BV(1);
//enable T0 intr in VIC
VICVectAddr1 = (uint32_t)timer_isr;
VICVectCntl1 = BV(VIC_TIMER0) | VIC_EN;
VICIntSelect &= ~BV(VIC_TIMER0);
VICIntEnable |= BV(VIC_TIMER0);
//enable timer counter
T0TCR = BV(0);
}
void timer_isr()
{
flag=1;
//clear intr in TIMER regrs
T0IR |= BV(0);
//clear intr in VIC
VICVectAddr = 0x00000000;
}
/* Main.c*/
int main (void)
{
int cnt=0;
char str[32];
timer_init();
lcd_init();
lcd_putstring(LCD_LINE1," *TIMER* ");
_delay_ms(1000);
str_printf(str,"Count:%d",cnt);
//lcd_putstring(LCD_LINE2,str);
while(1)
{
while(flag==0);
flag = 0;
cnt++;
str_printf(str,"Count:%d",cnt);
lcd_putstring(LCD_LINE2,str);
}
return 0;
}
I am programming an MSP430 microcontroller with the MSP430 LaunchPad Dev Kit and I am running into some problems on this simple code.
#include <msp430.h>
void Delay(void);
#define LED1 BIT0 //define LED1 as bit 0 (0x00)
#define LED2 BIT6 //define LED2 as bit 6 (0x40)
#define delayTime 20000 //define iTime as 20000
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //stop watchdog timer
P1DIR |= (LED1|LED2); //set P1.0 and P1.6 to output direction (P1.3 is naturally an input)
P1OUT |= LED1; //set P1.0 high so the LEDs will blink alternatively
while(1)
{
Delay();
P1OUT ^= (LED1|LED2); //toggle P1.0 using exclusive-OR
}
}
void Delay(void)
{
int i = 0;
while(delayTime > i)
{
i++;
}
}
This code compiles fine, but when debugging the code, the function call 'Delay()' is skipped entirely and the function is never entered. However, when I give the function a return type of 'unsigned int' like this:
unsigned int Delay(void)
{
int i = 0;
while(delayTime > i)
{
i++;
}
return 1;
}
I can call the Delay function in an if statement like the one below and the debugger will enter the function.
if(Delay() == 1)
{
P1OUT ^= (LED1|LED2); //toggle P1.0 using exclusive-OR
}
I'm sure there is some simple oversight that I'm making. I can't for the life of me figure out why the debugger is skipping my first void function call. Any wisdom?
swineone has responded with the following correct solution in a comment:
"Try changing the declaration int i = 0; to volatile int i = 0; in the
Delay() function. This tells the optimizer not to touch that variable,
and may be the difference between the optimizer optimizing the code
away or not."
Thanks for the help!
It's recommended to work with interrupts. Such a task goes to this:
#include "io430.h"
#define ON 1
#define OFF 0
#define LED1 P1OUT_bit.P0
#define LED2 P1OUT_bit.P6
void init(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1OUT = 0x00;
P1DIR = 0xFF;
// initialize Timer0_A
TA0CCR0 = 62500; // set up terminal count
TA0CTL = TASSEL_2 + ID_3 + MC_1; // configure and start timer
// enable interrupts
TA0CCTL0_bit.CCIE = 1; // enable timer interrupts
__enable_interrupt(); // set GIE in SR
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void myTimerISR(void)
{
LED1 = ~LED1;
}