Rcario
Newbie level 4
Hello everyone, I'm trying to send signals to ATMEGA32 using serial port in order to control motors connected with any port, probably PORTA. The problem is my sample code runs in simulation but doesn't work in real time hardware. Can anybody help me what are the issues associated with this kind of problem and how can i overcome this. I've tested that the data sending procedure with serial port via MATLAB is 100% working and I've a doubt that if i'm missing something important on my codes. Please help.
My code goes like this:
My code goes like this:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <avr/io.h> #include <util/delay.h> #define baud 9600 #define my_ubbr F_CPU/16/baud-1 void usart_init(uint16_t ubrr); char usart_getchar( void ); void usart_putchar(char data); void usart_pstr(char *s); int main(void) { DDRA=0x07; // set port PA0, PA1 and PA2 as output // fire up the usart usart_init (my_ubbr); // set serial communication char receive; while(1) { receive=usart_getchar(); switch (receive) { case 'a': PORTA=0x01; // Turn On PA0 bit break; case 'b': PORTA=0x02; // Turn On PA1 bit break; case 'c': PORTA=0x04; // Turn On PA2 bit break; } _delay_ms(10); } } void usart_init( uint16_t ubrr) { // Set baud rate UBRRH = (uint8_t)(ubrr>>8); UBRRL = (uint8_t)ubrr; // Enable receiver and transmitter UCSRB = (1<<RXEN)|(1<<TXEN); // Set frame format: 8data, 2stop bit UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); } char usart_getchar(void) { // Wait for incoming data while ( !(UCSRA & (_BV(RXC))) ); // Return the data return UDR; }
Last edited by a moderator: