سلام خدمت اساتید عزیز....
من میخوام ATMEGA16A رو با یه ماژول آماده FTDI وصل کنم به پورت سریال ، این تصویر مدارم هستش:
http://s6.picofile.com/file/8198579426/madar.jpg
این هم کد پروژم:
اینم کتاب خانه ای که ساختم برای UART:
من میخوام ATMEGA16A رو با یه ماژول آماده FTDI وصل کنم به پورت سریال ، این تصویر مدارم هستش:
http://s6.picofile.com/file/8198579426/madar.jpg
این هم کد پروژم:
کد:
#define F_CPU 8000000 #include <avr/io.h> #include <util/delay.h> #include <Angel/UARTMethods.h> #include <avr/interrupt.h> int main(void) { InitializeUART(9600, 0, 8, NONE, 1, TRUE); while(1) { _delay_ms(100); TransmitUART('a'); } }
کد:
#ifndef UARTInit #define UARTInit #define EVEN 0 #define ODD 1 #define NONE 2 #define FALSE 0 #define TRUE 1 #include <math.h> #include <avr/io.h> unsigned char ReceiveUART(void) { while (! (UCSRA & (1 << RXC)) ); return UDR; } void TransmitUART(unsigned char data) { //Wait until the Transmitter is ready while (! (UCSRA & (1 << UDRE)) ); //Get that data outa here! UDR = data; } void InitializeUART(int baud, char AsyncDoubleSpeed, char DataSizeInBits, char ParityEVENorODD, char StopBits, char USARTInterruptEnable) { uint16_t UBBRValue = lrint(( F_CPU / (16L * baud) ) - 1); if (AsyncDoubleSpeed == 1) UCSRA = (1 << U2X); //setting the U2X bit to 1 for double speed asynchronous //Put the upper part of the baud number here (bits 8 to 11) UBRRH = (unsigned char) (UBBRValue >> 8); //Put the remaining part of the baud number here UBRRL = (unsigned char) UBBRValue; //Enable the receiver and transmitter UCSRB = (1 << RXEN) | (1 << TXEN); if (USARTInterruptEnable) UCSRB |= (1 << RXCIE); //Set 2 stop bits if (StopBits == 2) UCSRC = (1 << USBS); if (ParityEVENorODD == EVEN) UCSRC |= (1 << UPM1); //Sets parity to EVEN if (ParityEVENorODD == ODD) UCSRC |= (3 << UPM0); //Alternative way to set parity to ODD if (DataSizeInBits == 6) UCSRC |= (1 << UCSZ0); //6-bit data length if (DataSizeInBits == 7) UCSRC |= (2 << UCSZ0); //7-bit data length if (DataSizeInBits == 8) UCSRC |= (3 << UCSZ0); //8-bit data length if (DataSizeInBits == 9) UCSRC |= (7 << UCSZ0); //9-bit data length } #endif
دیدگاه