Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

AVR studio gcc serial interrupt error

Status
Not open for further replies.

drbizzarow

Advanced Member level 1
Joined
May 24, 2006
Messages
414
Helped
25
Reputation
50
Reaction score
15
Trophy points
1,298
Activity points
3,662
hi guys !!!

i have written a code in which for atmega162...... every time its receive a byte my controller hangs..... can any body tell me what i am missing ???

#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

/* CPU frequency */
#define F_CPU 16000000UL
#define USART_BAUDRATE 9600

#define BAUD_PRESCALE (((F_CPU) / (USART_BAUDRATE * 16UL)) - 1)

#define HIGH 1
#define LOW 0
#define LEFT 'L'
#define RIGHT 'R'
#define FRONT 'F'
#define BACK 'B'
#define KNEE 'K'
//#define STOP 'S'
//#define All 'A'
//#define VOID_SPACE 'V'

#define SERIAL_PACKET_SIZE 10
#define DISTANCE_THRESHOLD 50

#define BUZZER_ON PORTB |= 0x01; //Buzzer 0N
#define BUZZER_OFF PORTB &= 0xFE; //Buzzer OFF

#define SCAN_MTR_LEFT PORTC = PORTC |= 0x03; PORTC &= 0xFD;
#define SCAN_MTR_RIGHT PORTC = PORTC |= 0x03; PORTC &= 0xFE;
#define SCAN_MTR_STOP PORTC = PORTC |= 0x03;

#define LEFT_MOTOR_OFF PORTC |= 0x08;
#define LEFT_MOTOR_ON PORTC &= 0xF7;

#define FRONT_MOTOR_OFF PORTC |= 0x04;
#define FRONT_MOTOR_ON PORTC &= 0xFB;

#define RIGHT_MOTOR_OFF PORTC |= 0x20;
#define RIGHT_MOTOR_ON PORTC &= 0xDF;

#define BACK_MOTOR_OFF PORTC |= 0x10;
#define BACK_MOTOR_ON PORTC &= 0xEF;

#define KNEE_MOTOR_OFF PORTC |= 0x40;
#define KNEE_MOTOR_ON PORTC &= 0xBF;

#define AUDIO_FOR_FRONT PORTB = 0xFF;
#define AUDIO_FOR_BACK PORTB = 0xFF
#define AUDIO_FOR_LEFT PORTB = 0xFF
#define AUDIO_FOR_RIGHT PORTB = 0xFF
#define AUDIO_FOR_KNEE PORTB = 0xFF
#define PLAY_AUDIO PORTB = 0xFE; _delay_ms(50); PORTB = 0xFF
#define NO_AUDIO PORTB = 0xFF

unsigned char SerialBuffIndex = 0;
unsigned char SerialBuff[SERIAL_PACKET_SIZE];
unsigned char CurDistance;
unsigned char CurDirection;
unsigned char SmtrDir;
unsigned char Tbyte;
unsigned char ObsDetect;

void ldelay(int time)
{
while(time--)
_delay_ms(10000);
}

ISR(USART0_RXC_vect)
{
unsigned char SerialCmd;

SerialCmd = UDR0;
}

int main(void)
{
DDRA = 0xFF;
DDRB = 0xFF;
DDRC = 0xFF;
DDRD = 0xFE;
DDRE = 0xFF;

PORTA = 0xFF;
PORTB = 0xFE;
PORTC = 0xFF;
PORTD = 0xFF;
PORTE = 0xFF;

DDRA = 0x00;
DDRE = 0x00;

UCSR0B |= (1 << RXEN0) | (1 << TXEN0); // Turn on the transmission and reception circuitry
UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01);// Use 8-bit character sizes
UBRR0L = 50;//BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRR0H = 0;//(BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register

UCSR0B |= (1 << RXCIE0); // Enable the USART Recieve Complete interrupt (USART_RXC)
sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
while(1)
{
UDR0 = 'H'; _delay_ms(100);
UDR0 = 'p'; _delay_ms(100);
UDR0 = 'V'; _delay_ms(100);
UDR0 = 'i'; _delay_ms(100);
UDR0 = 's'; _delay_ms(100);
}

BUZZER_OFF;
//All MOTORS OFF
SCAN_MTR_STOP

while(1)
{

}
return 0;
}
 

do you use a emulator or just program the mcu?

if you debug check if stops at the ISR(USART0_RXC_vect) ? when it goes after.

Check also cstack and rstack if low.
 

i am not using simulator i am direct programming mcu........

i try to ON the buzzer on the 1st line of ISR;but it is not turn on ...that means controller not jumping ISR it hangs before jump.
 

drbizzarow said:
Code:
ISR(USART0_RXC_vect) 
{
  unsigned char SerialCmd;
  SerialCmd = UDR0;
}

i am not using simulator i am direct programming mcu........
i try to ON the buzzer on the 1st line of ISR;but it is not turn on ...that means controller not jumping ISR it hangs before jump.

Since you don't have an emulator it is clever to use the buzzer for debugging purpose. But where exactly do you drive the buzzer?
Alexandros
 

yes this is not mentioned on above code but i have try it before ....... result was same as i mentioned above.

all code was same only use BUZZER_ON see below code.

ISR(USART0_RXC_vect)
{
unsigned char SerialCmd;

BUZZER_ON
SerialCmd = UDR0;

}
 

drbizzarow said:
all code was same only use BUZZER_ON

OK then. The truth is that without emulator things are a bit difficult when it comes to bugs detection, but buzzer is good to observe code behaviour. So try to set the buzzer on, at different places. After while(1) for example. If you are listening to a sound, then code passes from there and so you can place the BUZZER_ON command at the end of main, to see if the loop is completed succesfully. If you find out that loop is executed, then you can place BUZZER_ON inside the ISR again. Most likely the loop is executed but you cannot communicate effectively.
What kind of communication are you implementing? Between two MCUs, MCU with PC, MCU with another device? You could also post a schematic, maybe your code is OK and you are facing hardware problems. What is the level of code optimization? You also mentioned code hanging. Can you please be more descriptive about it?

Code:
ISR(USART0_RXC_vect)
{
  unsigned char SerialCmd;
  BUZZER_ON
  SerialCmd = UDR0;
}

Finally, as you can realize this is dummy code. UDR0 is saved in a local variable and when the ISR is executed this value is lost.

Hope this helps.
Alexandros
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top