minato91
20-03-2012, 03:56 PM
Chào mọi người,mình là thành viên mới của diễn dàn, cũng là người mới làm quen với PIC.
Cho mình hỏi một vấn đề về code C điều khiển hiển thị nhiệt độ LM35 :
Code nó là:
#define <16F877A.h>
#include "16F877A.h"
#device *=16 adc=8
#use i2c(Master,slow,sda=PIN_C4,scl=PIN_C3)
#byte PORTB = 0x06 //khai báo portb
#bit SER = 0x06.0 //DUA DU LIEU VAO
#bit SCK = 0x06.1 //Chan cap xung duong de dua data vào trong thanh ghi dich
#bit SCLR = 0x06.2 //KHI DUA CHAN SCLR VE MUC THAP,IC SE XOA DU LIEU TRONG THANH GHI DICH
#bit RCK = 0x06.3 //Khi data da vào trong thanh ghi dich, dua 1 xung ra chân này de dua data tu thanh ghi dich ra
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
int8 second, minute, hour;
#INT_EXT
int8 read;
void laysohang(int sohang[],int x)
{
sohang[0]=x%10;
x=x/10;
sohang[1]=x%10;
}
void Out_595(unsigned char num1,unsigned char num2)
{
char i;
int16 data;
data =num1;
data =(data<<8)+num2;
SCLR = 1; //cam xuat du lieu ra khi chua chuyen doi xong
for(i = 0;i<16;i++)
{
if(data & 0x8000)//truyen bit co trong so cao truoc
SER = 1;
else
SER = 0;
SCK = 1; //DUA 1 XUNG DUONG VAO CHAN SCK DE DUA DU LIEU VAO TRONG IC
SCK = 0; //DUA CHAN SCLR VE LAI MUC THAP
data =data<<1; //dich tra 1 bit
}
RCK = 1;
RCK = 0;
SCLR = 0; //cho phep xuat du lieu ra
}
void Init_595(void)
{
RCK = 0;
SCK = 0;
SER = 0;
SCLR = 1;
}
void main()
{
set_tris_a(0xFF);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x00);
unsigned char so[10] = {0b01000000,0b01111001,0b00100100,0b00110000,
0b00011001,0b00010010,0b00000010,0b01111000,0b0000 0000,0b00010000};
int digit[2];
// Khoi tao cho ngat ngoai
enable_interrupts (INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
//Khoi tao ADC
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(50);
//LCD_INIT();
//printf(lcd_putc,"Time:");
while(1)
{
read=read_adc();
read =read*1.960784314;
laysohang(digit,read);
Init_595();
Out_595(so[digit[0]],so[digit[1]]);
//second = BCDtoDEC(read_ds1307(0x00));
// minute = BCDtoDEC(read_ds1307(0x01));
// hour = BCDtoDEC(read_ds1307(0x02));
/// lcd_gotoxy(7,1);
//printf(lcd_putc,"%02d:%02d:%02d",hour,minute,second);
delay_ms(1000);
}
}
Mình không hiểu vì sao điều chỉnh nút bấm 2 lần thì đèn LED mới thông báo một lấn.Hay là hàm read =read*1.960784314; của mình bị sai. Mong mọi người sữa giúp.
Thanks all!
Cho mình hỏi một vấn đề về code C điều khiển hiển thị nhiệt độ LM35 :
Code nó là:
#define <16F877A.h>
#include "16F877A.h"
#device *=16 adc=8
#use i2c(Master,slow,sda=PIN_C4,scl=PIN_C3)
#byte PORTB = 0x06 //khai báo portb
#bit SER = 0x06.0 //DUA DU LIEU VAO
#bit SCK = 0x06.1 //Chan cap xung duong de dua data vào trong thanh ghi dich
#bit SCLR = 0x06.2 //KHI DUA CHAN SCLR VE MUC THAP,IC SE XOA DU LIEU TRONG THANH GHI DICH
#bit RCK = 0x06.3 //Khi data da vào trong thanh ghi dich, dua 1 xung ra chân này de dua data tu thanh ghi dich ra
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
int8 second, minute, hour;
#INT_EXT
int8 read;
void laysohang(int sohang[],int x)
{
sohang[0]=x%10;
x=x/10;
sohang[1]=x%10;
}
void Out_595(unsigned char num1,unsigned char num2)
{
char i;
int16 data;
data =num1;
data =(data<<8)+num2;
SCLR = 1; //cam xuat du lieu ra khi chua chuyen doi xong
for(i = 0;i<16;i++)
{
if(data & 0x8000)//truyen bit co trong so cao truoc
SER = 1;
else
SER = 0;
SCK = 1; //DUA 1 XUNG DUONG VAO CHAN SCK DE DUA DU LIEU VAO TRONG IC
SCK = 0; //DUA CHAN SCLR VE LAI MUC THAP
data =data<<1; //dich tra 1 bit
}
RCK = 1;
RCK = 0;
SCLR = 0; //cho phep xuat du lieu ra
}
void Init_595(void)
{
RCK = 0;
SCK = 0;
SER = 0;
SCLR = 1;
}
void main()
{
set_tris_a(0xFF);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x00);
unsigned char so[10] = {0b01000000,0b01111001,0b00100100,0b00110000,
0b00011001,0b00010010,0b00000010,0b01111000,0b0000 0000,0b00010000};
int digit[2];
// Khoi tao cho ngat ngoai
enable_interrupts (INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
//Khoi tao ADC
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(50);
//LCD_INIT();
//printf(lcd_putc,"Time:");
while(1)
{
read=read_adc();
read =read*1.960784314;
laysohang(digit,read);
Init_595();
Out_595(so[digit[0]],so[digit[1]]);
//second = BCDtoDEC(read_ds1307(0x00));
// minute = BCDtoDEC(read_ds1307(0x01));
// hour = BCDtoDEC(read_ds1307(0x02));
/// lcd_gotoxy(7,1);
//printf(lcd_putc,"%02d:%02d:%02d",hour,minute,second);
delay_ms(1000);
}
}
Mình không hiểu vì sao điều chỉnh nút bấm 2 lần thì đèn LED mới thông báo một lấn.Hay là hàm read =read*1.960784314; của mình bị sai. Mong mọi người sữa giúp.
Thanks all!