◀ Previous Next ▶ Up ▲


witpoko.com


ช่วยหนูแก้โปรแกรมนี้หน่อยค่ะ (PIC)

ใช้ CCS C เขียนค่ะ
ใช้ PIC16F877+7-segments 4 ตัว+DS1820 เพื่อวัดอุณหภูมิมาโชว์บน 7-segments เช่น 29.5C น่ะค่ะ
ต่อขาตามนี้นะคะ
LCD --> RB0-RB7 เป็น data , RA0-RA3 เป็น control แต่ละ หลักของ 7-segments

#include <16F877.h>

#define TxD PIN_C6
#define RxD PIN_C7
#define CLOCK_SP 20000000

#fuses HS
#fuses NOLVP, NOWDT
#fuses NOPROTECT
#use delay (clock=CLOCK_SP)
#use rs232 (baud=9600, xmit=TxD, rcv=RxD)

#byte port_b = 6

char num_led[17] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, // 0,1,2,3,4
0x6D, 0x7D, 0x07, 0x7F, 0x6F, // 5,6,7,8,9
0x77, 0x7C, 0x39, 0x5E, 0x79, // A,b,C,d,E
0x71, 0x80}; // F,.

// Dallas Touch Driver
// present = touch_present() Issues a reset and returns TRUE
// if the touch device is there.
// data = touch_read_byte() Reads one byte from a touch device.
// ok = touch_write_byte(data) Writes one byte to a touch device
// and returns TRUE if all went OK.
// A FALSE indicates a collision with
// another device.

// Begin "Dallas Touch Driver"

#define TOUCH_PIN PIN_A4

byte touch_read_byte() {
byte i,data;

for(i=1;i<=8;++i) {
output_low(TOUCH_PIN);
delay_us(14);
output_float(TOUCH_PIN);
delay_us(5);
shift_right(&data,1,input(TOUCH_PIN));
delay_us(100);
}
return(data);
}

byte touch_write_byte(byte data)
{
byte i;
for(i=1;i<=8;++i)
{
output_low(TOUCH_PIN);
delay_us(10);
if(shift_right(&data,1,0))
{
output_high(TOUCH_PIN);
}
else
{
output_low(TOUCH_PIN);
}
delay_us(50);
output_high(TOUCH_PIN);
delay_us(50);
}
return(TRUE);
}

byte touch_present() {
boolean present;

output_low(TOUCH_PIN);
delay_us(500);
output_float(TOUCH_PIN);
delay_us(5);
if(!input(TOUCH_PIN))
return(FALSE);
delay_us(65);
present=!input(TOUCH_PIN);
delay_us(240);
if(present)
return(TRUE);
else
return(FALSE);
}

// End "Dallas Touch Driver"

byte ReadTemp_DS1820()
{
byte i;
byte buffer[9];
byte temp_msb, temp_lsb, temp_real;

if (touch_present())
{
touch_write_byte (0xCC);
touch_write_byte (0x44);
delay_ms (200);
touch_present();
touch_write_byte (0xCC);
touch_write_byte (0xBE);

for (i=0; i<9; i++)
buffer[i] = touch_read_byte();

temp_lsb = buffer[0];
temp_msb = buffer[1];
temp_real = temp_lsb/2;

return temp_lsb;
}
}

void DspLED7Seg(int c1, int c2, int c3, int c4)
{
port_b = num_led[c4];
output_low (PIN_A3);
delay_ms (1);
output_high (PIN_A3);

port_b = num_led[c3];
output_low (PIN_A0);
delay_ms (1);
output_high (PIN_A0);

port_b = num_led[c2] | num_led[17];
output_low (PIN_A1);
delay_ms (1);
output_high (PIN_A1);

port_b = num_led[c1];
output_low (PIN_A2);
delay_ms (1);
output_high (PIN_A2);
}

void main()
{
int16 i;
int j,c1,c2,c3,c4;
byte temp_lsb;

set_tris_a (0x10);
set_tris_b (0x00);

while (TRUE)
{
temp_lsb = ReadTemp_DS1820();

c1 = ((int)temp_lsb/10)/2;
c2 = ((int)temp_lsb - (c1*20))/2;
c3 = (((int)temp_lsb - (c1*20))%2)*5;
c4 = 12;
for (j=0; j<200; j++)
DspLED7Seg(c1,c2,c3,c4);

delay_ms(1000);
}
}


ขอบคุณมากๆค่ะ


จากคุณ : สาวน้อย -[ 5 ต.ค. 47 - 19:33:11 A:161.200.85.117 X:161.200.255.163 ]





กระทู้ยอดนิยม