Anh Nam a , day la ctr cua em, em hien thi tren led 7 doan, phan hien thi ko co gi , chan cua ad7715 em noi chan nhu trong huong dan. day la code:
Code:
#include <16f877.h>
#include <defs_16f877A.h>
//#include <AD7715.c>
//#device *=16 ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT//HS,NOWDT,NOLVP,NOPUT, NOCPD, NOWRT, NOBROWNOUT, NOPROTECT //NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=11059200) // Su dung Fuse nay thi co nhieu , ko biet tai sao?
#use fast_io(b)
#use fast_io(d)
#use fast_io(c)
int8 const BCD2LED[]={ 0x41, 0xED, 0x54, 0xC4, 0xE8,\
0xC2, 0x42, 0xE5, 0x40, 0xC0, };
int8 COUNT1 ,COUNT2;
int16 COUNTX,T1,T2; // Bien trong ngat
int16 BUFF[4], Y0, Y1; // Bo dem de hien thi so tat hay khong
int8 BCD[4];
int16 value;
//-------------------------------------------------------------//
#define LED1 RB4 // =0 thi LED sang
#define LED2 RB5
#define LED3 RB6
#define LED4 RB7
//-----------
#define ADC_CS RC7 // Noi dat GND
#define ADC_RESET RC6 // Noi VCC
#define ADC_DRDY RC2
#define ADC_DI RC4
#define ADC_DO RC5
#define ADC_CLK RC3
Void LamTuoi()
{
if(COUNT1 >=4) {COUNT1 = 0; }
PORTB = 0xf0;
PORTD = BCD2LED[BCD[COUNT1]]; // CHOT*/
switch (COUNT1) {
case 0: LED1 =0; //bat led1
break;
case 1: LED2 =0; //bat led2
break;
case 2: LED3 =0; //bat led3
break;
case 3: LED4 =0; //bat led4
break;
}
COUNT1 ++;
}
#int_timer0
void ngat()
{
Set_timer0(0);
COUNT2 ++;
if (Count2 == 10){
Count2 = 0;
LamTuoi();
//COUNTX = read_adc();
}
}
void InBCD(int8 value4,int8 value3,int8 value2,int8 value1)
{
BCD[0]= value4;
BCD[1]= value3;
BCD[2]= value2;
BCD[3]= value1;
}
void LedView( int16 Value) // DUA SO VAO DE HIEN THI
{
BUFF[3] = Value/1000;
Y0 = Value%1000;
BUFF[2] = Y0/100;
Y1 = Y0%100;
BUFF[1] = Y1/10;
BUFF[0] = Y1%10;
InBCD(BUFF[3],BUFF[2],BUFF[1],BUFF[0]);
}
void write_adc_byte(Byte data);
adc_init()
{
ADC_RESET = 0;
ADC_CLK =1;
ADC_CS =1; //Set low to AD7715 chip select low pin
ADC_RESET =1; //Set high to AD7715 reset low pin
delay_ms(30);
write_adc_byte( 0x12 ); //Communications Register
write_adc_byte( 0x62 ); //Setup Register info here
}
void write_adc_byte(BYTE data) {
int8 i;
ADC_CS =0;
for(i=1;i<=8;++i) {
ADC_CLK =0;
output_bit(PIN_C4,shift_left(&data,1,0)); // Output the MSB of data to
// DI and at the same time shift 0 into the LSB of data
ADC_CLK =1;
}
ADC_CS =1;
}
int16 read_adc_word() {
int8 i;
BYTE data;
ADC_CS =0;
for(i=1;i<=16;++i) {
ADC_CLK =0;
ADC_CLK =1;
shift_left(&data,2,input(PIN_C5));
}
ADC_CS =1;
return data;
}
int16 read_adc_value() {
int16 value; //long mac dinh kieu int16
while ( input(PIN_C2) );
while ( !input(PIN_C2) );
while ( input(PIN_C2) );
write_adc_byte(0x3a);
value=read_adc_word();
// return value;
}
void KhoiTao()
{
set_tris_b(0b00001111); // 1 Dau vao =0 la dau ra
set_tris_d(0); // ra
set_tris_c(0b11111111);
enable_interrupts(global);
//enable_interrupts(int_ext);
enable_interrupts(int_timer0);
Setup_timer_0(RTCC_INTERNAL| RTCC_DIV_4);
Set_timer0(0) ; //vao
Value=0;
Delay_ms(10);
}
//-------------------------------------------------------------
void main()
{ Khoitao();
adc_init();
ledview(Value);
Lamtuoi();
while (1) {
read_adc_value() ;
ledview(Value);
Lamtuoi();
}
}