Video test mạch :
Code của mạch : viết trên phần mềm CCS
#include <16F877A.h>
#include <string.h>
#include <stdlib.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(crystal=12000000)
#byte T1CON = 0x10
#define S2 PIN_C2
#define S3 PIN_C3
float tanso=0,F_R=0,F_B=0,F_G=0,F_C=0;
char R=0,B=0,G=0;
int1 chopheptinh=0;
void mau();
char menu=1;
////////////////////////////////////////////////////////
#define lcd_type 2
#define lcd_line_two 0x40
#define RS PIN_A0
#define RW PIN_A1
#define ENABLE PIN_A2
BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
//////////////////////////////////////////////////////////////////////////
void lcd_send_nibble( BYTE n ) {
if(n&0x01)output_high(PIN_A3);
else output_low(PIN_A3);
if(n&0x02)output_high(PIN_A4);
else output_low(PIN_A4);
if(n&0x04)output_high(PIN_A5);
else output_low(PIN_A5);
if(n&0x08)output_high(PIN_E0);
else output_low(PIN_E0);
delay_us(50);
output_high(ENABLE);
delay_us(50);
output_low(ENABLE);
delay_ms(1);
}
void lcd_send_byte( BYTE address, BYTE n ) {
output_low(RS);
if(address)output_high(RS);
else output_low(RS);
delay_us(50);
output_low(RW);
delay_us(50);
output_low(ENABLE);
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
delay_ms(1);
}
void lcd_init() {
BYTE i;
output_low(RS);
output_low(RW);
output_low(ENABLE);
delay_ms(50);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(10);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0,LCD_INIT_STRING[i]);
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1); break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
//////////////////////////////////////////////////////////////////////////
#INT_RTCC
void RTCC_isr(void)
{
tanso=get_timer1()*45.87156;
mau();
set_timer1(0);
set_timer0(0);
}
void main()
{
set_tris_a(0x00);
set_tris_c(0xff);
output_a(0x00);
output_low(S2);
output_low(S3);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit); //21.8 ms overflow
T1CON=0x0B;
set_timer0(0);
set_timer1(0);
lcd_init();
lcd_gotoxy(1,1);
lcd_putc("....TSC3200.....\n");
lcd_putc("Phan Hoang Thach");
delay_ms(1000);
output_low(S2);
output_low(S3);
lcd_putc("\f");
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
char str[3];
char so=0;
while(true)
{
if(chopheptinh==1)
{
R=(F_R*100)/F_C;
B=(F_B*100)/F_C;
G=(F_G*100)/F_C;
if((R>=69)&&(R<=79)&&(B>=18)&&(B<=25)&&(G>=13)&&(G<=18))so=1;//do
else if((R>=50)&&(R<=68)&&(B>=17)&&(B<=25)&&(G>=20)&&(G<=25))so=2;//cam
else if((R>=7)&&(R<=14)&&(B>=58)&&(B<=66)&&(G>=27)&&(G<=32))so=3;//xanh duong
else if((R>=45)&&(R<=51)&&(B>=17)&&(B<=22)&&(G>=33)&&(G<=37))so=4;//vang
else if((R>=20)&&(R<=25)&&(B>=31)&&(B<=37)&&(G>=40)&&(G<=54))so=5;//xanh la
else so=0;
chopheptinh=0;
}
if(so==1) {lcd_gotoxy(1,0);lcd_putc(" Mau Do ");}
else if(so==2) {lcd_gotoxy(1,0);lcd_putc(" Mau Cam ");}
else if(so==3) {lcd_gotoxy(1,0);lcd_putc(" Mau Xanh Duong ");}
else if(so==4) {lcd_gotoxy(1,0);lcd_putc(" Mau Vang ");}
else if(so==5) {lcd_gotoxy(1,0);lcd_putc(" Mau Xanh La ");}
else {lcd_gotoxy(1,0);lcd_putc(" Khong xac dinh ");}
sprintf(str,"%u",R);
if(R<100)str[2]=' ';
if(R<10){str[2]=' ';str[1]=' ';}
lcd_gotoxy(1,1);
lcd_putc("R=");
lcd_gotoxy(3,1);
lcd_putc(str[0]);
lcd_gotoxy(4,1);
lcd_putc(str[1]);
lcd_gotoxy(5,1);
lcd_putc(str[2]);
sprintf(str,"%u",B);
if(B<100)str[2]=' ';
if(B<10){str[2]=' ';str[1]=' ';}
lcd_gotoxy(6,1);
lcd_putc("B=");
lcd_gotoxy(8,1);
lcd_putc(str[0]);
lcd_gotoxy(9,1);
lcd_putc(str[1]);
lcd_gotoxy(10,1);
lcd_putc(str[2]);
sprintf(str,"%u",G);
if(G<100)str[2]=' ';
if(G<10){str[2]=' ';str[1]=' ';}
lcd_gotoxy(11,1);
lcd_putc("G=");
lcd_gotoxy(13,1);
lcd_putc(str[0]);
lcd_gotoxy(14,1);
lcd_putc(str[1]);
lcd_gotoxy(15,1);
lcd_putc(str[2]);
}
}
void mau()
{
switch(menu)
{
case 1://red
{
output_low(S2);
output_low(S3);
F_R=tanso;
menu=2;
output_low(S2);
output_high(S3);
break;
}
case 2://blue
{
output_low(S2);
output_high(S3);
F_B=tanso;
menu=3;
output_high(S2);
output_high(S3);
break;
}
case 3://green
{
output_high(S2);
output_high(S3);
F_G=tanso;
menu=4;
output_high(S2);
output_low(S3);
break;
}
case 4:
{
output_high(S2);
output_low(S3);
F_C=tanso;
chopheptinh=1;
menu=1;
output_low(S2);
output_low(S3);
break;
}
}
}
Mạch phân biệt được 5 màu khác nhau , các bạn có thể mở rộng để phân biệt được nhiều màu khác nhau
Không có nhận xét nào:
Đăng nhận xét