we now make the connection between microcontrol pic 16f877a and atmega32 via uart
the code for pic micro c pro
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
char uart_rd,i=0;
void main() {
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
Lcd_Chr_Cp(uart_rd); // and send data via UART
i++;
if(i>=31) //clear lcd when it complet to the end by 'a'
Lcd_Cmd(_LCD_CLEAR);
}
}
}
the code for atmega32 over avr studio 5
/*
* test.c
*
* Created: 12/9/2013 3:49:05 AM
* Author: ahmed
*/
#include
#include
#define F_CPU 8000000ul
#define USART_BAUD 9600ul
#define USART_UBBR_VALUE ((F_CPU/(USART_BAUD<<4 4="" br="" nbsp="" was="">//UBBR for Asynchronous Double Speed Mode (U2X = 1)
// UBRR = (fOSC / 8*BAUD) -1
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);
void USART_Init(void)
{
UBRRH = (uint8_t)(USART_UBBR_VALUE>>8);
UBRRL = (uint8_t)USART_UBBR_VALUE;
UCSRC = (0<UCSRB = (1< 4>}
void USART_SendByte(uint8_t u8Data)
{
while((UCSRA&(1<UDR = u8Data;
}
uint8_t USART_ReceiveByte(void)
{
// Wait until a byte has been received
while((UCSRA&(1<
// Return received data
return UDR;
}
int main(void)
{
TCCR1B |= 1<// Initialize USART
USART_Init();
while(1)
{
if( TCNT1 > 122072/2) // one second 1000* 8,000,000/65,535
// max value 65535!!!!
{
TCNT1 = 0;
//Send string
USART_SendByte('a');
//u8Data = USART_ReceiveByte(); }
}
}
static int uart_putchar(char c, FILE *stream)
{
if (c == '\n') uart_putchar('\r', stream);
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<UDR = c;
return 0;
}
uint8_t uart_getchar(void)
{
/* Wait for data to be received */
while ( !(UCSRA & (1<return(UDR);
}
ليست هناك تعليقات:
إرسال تعليق