CODE:
/*****************************************************Project : Dem San Pham Hien Thi LCD & Giao Tiep UART
Version : 1.1
Date : 10/06/2012
Author : Doan Minh Quynh
Phan Duc Y
Company : DH LT TDH-K3
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
// External Interrupt 0 service routine
unsigned int xDV=999,xNG,xTR,xTY;
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
}
unsigned int xDV,xNG,xTR,xTY;
// ----------Ham Ngat INT1 (PD3)------------------//
interrupt [EXT_INT1] void ext_int1_isr(void) // Ham Su Dung Ngat PIN D.3
{
if(PIND.3==0)
{
xDV++;
if(xDV>999)
{
xNG++;
xDV=0;
}
if(xNG>999)
{
xTR++;
xNG=0;
}
if(xTR>999)
{
xTY++;
xTR=0;
}
}
}
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// ---------Khai Bao UART & Dung Ngat Nhan Tin Hieu ----------//
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
#include <stdio.h>
//------------------ Hien Thi LCD-----------------------------//
unsigned char ch,tr,dv;
void lcd_putnum(unsigned int so)
{
tr=(so/100);
ch=((so-tr*100)/10);
dv=(so-tr*100-ch*10);
lcd_putchar(tr+48);
lcd_putchar(ch+48);
lcd_putchar(dv+48);
}
void HienThi_LCD()
{
lcd_gotoxy(1,1);
lcd_putnum(xTY);
lcd_gotoxy(4,1);
lcd_putsf("'");
lcd_gotoxy(5,1);
lcd_putnum(xTR);
lcd_gotoxy(8,1);
lcd_putsf("'");
lcd_gotoxy(9,1);
lcd_putnum(xNG);
lcd_gotoxy(12,1);
lcd_putsf("'");
lcd_gotoxy(13,1);
lcd_putnum(xDV);
}
// --------------------Hien Thi UART ----------------------------//
unsigned char Uch,Utr,Udv;
void UART_putnum(unsigned int Uso)
{
Utr=(Uso/100);
Uch=((Uso-Utr*100)/10);
Udv=(Uso-Utr*100-Uch*10);
putchar(Utr+48);
putchar(Uch+48);
putchar(Udv+48);
}
void HienThi_UART()
{
printf("san pham = ");
UART_putnum(xTY);
printf(" ");
UART_putnum(xTR);
printf(" ");
UART_putnum(xNG);
printf(" ");
UART_putnum(xDV);
}
unsigned char RUN;
void main(void)
{
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0xFC;
DDRD=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: On
// INT1 Mode: Falling Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0A;
MCUCSR=0x00;
GIFR=0xC0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
//-------------------Canh Bao-------------------//
PORTA.0=RUN; // Den Bao Run
// Bao Full San Pham
if ((xDV==999)&&(xNG==999)&&(xTR==999)&&(xTY==999))
PORTA.1=1;
else PORTA.1=0;
//-------------------Chon Che Do-------------------//
if (PIND.4==0)
{
PORTA.3=1;
PORTA.2=0;
//-------------------DK Bang SW-------------------//
if (PIND.5==0)
{
RUN=1; // He Thong Run
lcd_clear();
}
if (PIND.6==0)
{
RUN=0; // He Thong Stop
lcd_clear();
}
switch (RUN) {
case 0: // He Thong Stop
{
xDV=0 ;
xNG=0 ;
xTR=0 ;
xTY=0 ;
lcd_gotoxy(0,0);
lcd_putsf("STOP >> SW");
lcd_gotoxy(0,1);
lcd_putsf("RUN AN 'START'");
printf("HE THONG 'STOP' >> ");
printf("KHOI DONG AN 'START'");
printf("\n\r");
}
break;
case 1: // He Thong Run
{
lcd_gotoxy(0,0);
lcd_putsf("San Pham: >> SW");
HienThi_LCD() ;
printf("Dieu Khien = 'BUTTON' >> ");
HienThi_UART();
printf("\n\r");
delay_ms(50);
}
break;
};
}
else
//-------------------DK Bang UART-------------------//
{
PORTA.3=0;
PORTA.2=1;
if (UDR==114) // 114=r (Run)
{
RUN=1; // He Thong Run
lcd_clear();
}
if (UDR==115) // 115=s (Stop)
{
RUN=0; // He Thong STOP
lcd_clear();
}
switch (RUN)
{
case 0: // He Thong Stop
{
xDV=0 ;
xNG=0 ;
xTR=0 ;
xTY=0 ;
lcd_gotoxy(0,0);
lcd_putsf("STOP >> PC");
lcd_gotoxy(0,1);
lcd_putsf("RUN AN 'r' <> ");
printf("HE THONG STOP >>");
printf("RUN AN 'r'");
printf("\n\r");
delay_ms(50);
}
break;
case 1: // He Thong Run
{
lcd_gotoxy(0,0);
lcd_putsf("San Pham: >> PC");
HienThi_LCD() ;
printf("Dieu Khien Bang 'PC' >> STOP An 's' ");
HienThi_UART();
printf("\n\r");
delay_ms(50);
}
break;
};
}
};
}
VIDEO DEMO
( Full code compiler + Simulation)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn codientu.org
0 Response to "Mạch đếm sản phẩm LCD + AVR Kết nối PC"
Đăng nhận xét