View Single Post
Old 16-11-2008, 02:54 PM   #11
humphuongbac
Nhập môn đệ tử
 
Tham gia ngày: Nov 2008
Bài gửi: 1
:
Các bác cho em hỏi chút.Em có down được một đoạn code (ngôn ngữ C) của mạch cảnh báo nhiệt độ. Và em chạy trên chương trình CCSC-3.249. Em cũng down cả file def_877A về và past trong thư mục devices. Nhưng khi compile nó báo lỗi là "A #DEVICE required before thí line", nghĩa là trước dòng #byte PORTA =0x05. Em ko biết phảo làm gì. Nhờ các bác chỉ giúp.
#include <C:\Program Files\PICC\Devices\16F877A.h> // Declare type of bit be used, file for bit, and important registers inside the PIC //
#include <def_877a.h> // Declare register's definition and important bits //
#device *=16 adc=10 // Declare pointer 16 bits and ADC 10 bits //
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT // Declare all configs //
#use delay(clock=20000000) // Declare function DELAY and frequency be used //
int8 high,low; // Declare interger variables 1 byte (8 bits)//
int8 const a[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
// Subprogram seperates tens and units into two variables that is declared above high and low //
void convert_bcd(int8 x)
{
low=x%10; // Divide to get remainder, low = units //
high=x/10;
high=high%10; // high = tens //
}
// Subprogram decode and display temperature //
void display()
{
PORTB=a[low]; // Send data to LED 1 //
RD0=0; // Turn on LED1, LED1 will display right value of low //
delay_ms(2); // delay 2 ms //
RD0=1; // Turn off LED1 //

PORTB=a[high]; // LED 2
RD1=0;
delay_ms(2);
RD1=1;
}
// Subprogram carries out warning or alarm //
void bao_dong(){
int8 i;
for(i=0;i<200;i++)
{
RE0=0;delay_us(100);
RE0=1;delay_us(100);
}
} // Sound 200 tongue //
// Main program //

void main() {
float value;
int16 i;

trisb = 0x00; // Setting up pins of PORTB is Output //
trisc = 0x00; // Setting up pins of PORTC is Output //
trisd = 0x00; // Setting up pins of PORTD is Output //
trise = 0x00; // Setting up pins of PORTE is Output //
trisa = 0xff; // Setting up pins of PORTa is Input //
portC = 0xff; // Setting up pins of PORTC that produce high level voltage //
portD = 0xff; // Setting up pins of PORTD that produce high level voltage //

// Setting up for ADC //
setup_adc(ADC_CLOCK_INTERNAL); // Showing operation method of ADC is sampling time by clock pulse //
// Setting up Analog signal pin is AN0 //
setup_adc_ports(AN0);
setup_ADC_channel(0)

delay_us(10); // delay 10 us //
value=(float)read_adc();
value = (value - 558.5)/2.048;
convert_bcd((int8)value);
i=0;
// infinite loop
while(1)
{
i++;
value = (float)read_adc();
value = (value - 558.5)/2.048; //for 5V supply
if (i==150) { convert_bcd((int8)value);i=0;}
if(((int8)value > 40) || ((int8)value < 15)) bao_dong();
display();
}
}
/*
--------------------------------------------------------------------------------------------------
Note:
Why we let a[low]==PORTB , LED 1 is low?
Because:
The constant array a[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 } will be equivalent with the following array of binary numbers a[10] = {11000000,11111001,10100100,10110000,10011001,1001 0010,10000010,11111000,10000000,10010000};
From right to left(ignore the last binary number because we don't use pin RB7 of PORTB),this binary numbers present voltage levels in a,b,c,d,e,f,g,h bars of LED. This array is correlative with the values 0,1,2,3,4,5,6,7,8,9 which display in LED

* Some functions of CCS be used are:
Function:
delay_us(time)
delay_ms(time)
Ex: delay_us(2) ; // making delay 2 us //
delay_ms(2); // making delay 2 ms //
Function:
setup_adc(ADC_CLOCK_INTERNAL);
This function is used to define operation method of ADC's transformer or this function is to define sampling time by clock pulse.

Function:
setup_adc_ports(AN0);
setup_ADC_channel(0)
These functions is used to define Analog signal pins which are pin AN0

This is definition file " def_877a.h" which we have declared before:
---------------------------------------------------------------------------------------------------
// register definitions
*/
#define W 0
#define F 1

// register files
#byte INDF =0x00
#byte TMR0 =0x01
#byte PCL =0x02
#byte STATUS =0x03
#byte FSR =0x04
#device *=14 adc=8
#byte PORTA =0x05
#byte PORTB =0x06
#byte PORTC =0x07
#byte PORTD =0x08
#byte PORTE =0x09

#byte EEDATA =0x10C
#byte EEADR =0x10D
#byte EEDATH =0x10E
#byte EEADRH =0x10F
#byte ADCON0 =0x1F
#byte ADCON1 =0x9F
#byte ADRESH =0x9F
#byte ADSESL =0x9F

#byte PCLATH =0x0a
#byte INTCON =0x0b
#byte PIR1 =0x0c
#byte PIR2 =0x0d
#byte PIE1 =0x8c
#byte PIE2 =0x8d
#byte OPTION_REG =0x81
#byte TRISA =0x85
#byte TRISB =0x86
#byte TRISC =0x87
#byte TRISD =0x88
#byte TRISE =0x89

#byte EECON1 =0x18C
#byte EECON2 =0x18D

#byte SSPBUF =0x13
#byte SSPCON =0x14
#byte SSPCON2 =0x91
#byte SSPADD =0x93
#byte SSPSTAT =0x94
// SSPCON bit
#bit SSPWCOL = 0x14.7
#bit SSPOV = 0x14.6
#bit SSPEN = 0x14.5
#bit SSPCKP = 0x14.4
#bit SSPM3 = 0x14.3
#bit SSPM2 = 0x14.2
#bit SSPM1 = 0x14.1
#bit SSPM0 = 0x14.0
// SSPSTAT bit
#bit SSPSMP = 0x94.7
#bit SSPCKE = 0x94.6
#bit SSPDA = 0x94.5
#bit SSPP = 0x94.4
#bit SSPS = 0x94.3
#bit SSPRW = 0x94.2
#bit SSPUA = 0x94.1
#bit SSPBF = 0x94.0

// BIT DEFINITION
#bit ra5 =0x05.5
#bit ra4 =0x05.4
#bit ra3 =0x05.3
#bit ra2 =0x05.2
#bit ra1 =0x05.1
#bit ra0 =0x05.0

#bit rb7 =0x06.7
#bit rb6 =0x06.6
#bit rb5 =0x06.5
#bit rb4 =0x06.4
#bit rb3 =0x06.3
#bit rb2 =0x06.2
#bit rb1 =0x06.1
#bit rb0 =0x06.0
#bit rC7 =0x07.7
#bit rC6 =0x07.6
#bit rC5 =0x07.5
#bit rC4 =0x07.4
#bit rC3 =0x07.3
#bit rC2 =0x07.2
#bit rC1 =0x07.1
#bit rC0 =0x07.0

#bit rD7 =0x08.7
#bit rD6 =0x08.6
#bit rD5 =0x08.5
#bit rD4 =0x08.4
#bit rD3 =0x08.3
#bit rD2 =0x08.2
#bit rD1 =0x08.1
#bit rD0 =0x08.0
#bit rE2 =0x09.2
#bit rE1 =0x09.1
#bit rE0 =0x09.0


#bit trisa5 =0x85.5
#bit trisa4 =0x85.4
#bit trisa3 =0x85.3
#bit trisa2 =0x85.2
#bit trisa1 =0x85.1
#bit trisa0 =0x85.0

#bit trisb7 =0x86.7
#bit trisb6 =0x86.6
#bit trisb5 =0x86.5
#bit trisb4 =0x86.4
#bit trisb3 =0x86.3
#bit trisb2 =0x86.2
#bit trisb1 =0x86.1
#bit trisb0 =0x86.0
#bit trisc7 =0x87.7
#bit trisc6 =0x87.6
#bit trisc5 =0x87.5
#bit trisc4 =0x87.4
#bit trisc3 =0x87.3
#bit trisc2 =0x87.2
#bit trisc1 =0x87.1
#bit trisc0 =0x87.0

#bit trisd7 =0x88.7
#bit trisd6 =0x88.6
#bit trisd5 =0x88.5
#bit trisd4 =0x88.4
#bit trisd3 =0x88.3
#bit trisd2 =0x88.2
#bit trisd1 =0x88.1
#bit trisd0 =0x88.0

#bit trise2 =0x89.2
#bit trise1 =0x89.1
#bit trise0 =0x89.0

// INTCON Bits for C
#bit gie = 0x0b.7
#bit peie = 0x0b.6
#bit tmr0ie = 0x0b.5
#bit int0ie = 0x0b.4
#bit rbie = 0x0b.3
#bit tmr0if = 0x0b.2
#bit int0if = 0x0b.1
#bit rbif = 0x0b.0

// PIR1 for C
#bit pspif = 0x0c.7
#bit adif = 0x0c.6
#bit rcif = 0x0c.5
#bit txif = 0x0c.4
#bit sspif = 0x0c.3
#bit ccp1if = 0x0c.2
#bit tmr2if = 0x0c.1
#bit tmr1if = 0x0c.0
//PIR2 for C
#bit cmif = 0x0d.6
#bit eeif = 0x0d.4
#bit bclif = 0x0d.3
#bit ccp2if = 0x0d.0
// PIE1 for C
#bit adie = 0x8c.6
#bit rcie = 0x8c.5
#bit txie = 0x8c.4
#bit sspie = 0x8c.3
#bit ccp1ie = 0x8c.2
#bit tmr2ie = 0x8c.1
#bit tmr1ie = 0x8c.0

//PIE2 for C
#bit osfie = 0x8d.7
#bit cmie = 0x8d.6
#bit eeie = 0x8d.4

// OPTION Bits
#bit not_rbpu = 0x81.7
#bit intedg = 0x81.6
#bit t0cs = 0x81.5
#bit t0se = 0x81.4
#bit psa = 0x81.3
#bit ps2 = 0x81.2
#bit ps1 = 0x81.1
#bit ps0 = 0x81.0
// EECON1 Bits
#bit eepgd = 0x18c.7
#bit free = 0x18C.4
#bit wrerr = 0x18C.3
#bit wren = 0x18C.2
#bit wr = 0x18C.1
#bit rd = 0x18C.0

//ADCON0
#bit CHS0 =0x1F.3
#bit CHS1 =0x1F.4
#bit CHS2 =0x1F.5
humphuongbac vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn