Ccs c for pic16F877a mục lục I. Tổng quan về ccs vì sao ta sử dung ccs ?



tải về 1.15 Mb.
trang2/15
Chuyển đổi dữ liệu23.08.2016
Kích1.15 Mb.
#26977
1   2   3   4   5   6   7   8   9   ...   15

1.5. Điều khiển led sáng dồn

//Chuong trinh led sang don o PORTB

/*ket qua o PORTB là:00000001,00000010,00000100,00001000,00010000,00100000,01000000,10000000,10000001,10000010,10000100,10001000,.....cuoi cung thi PORTB=0xFF */

#include<16F877A.h>

#include

#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP

#use delay(clock=20000000)

#use fast_io(b)

int8 sck,bienxoay;

main()


{

trisb=0;


while(true){

sck=8;


portb=0;

delay_ms(100);

bienxoay=1;

while(sck>0)

{

portb=bienxoay;



bienxoay=bienxoay<<1;

delay_ms(100);

sck--;

}

}



}
1.6. I/O + Delay _ Delay 1s RB0

#include <16F877A.h>

#fuses NOWDT,PUT,XT,NOPROTECT

#use delay(clock=4000000)

#byte PORTB = 0x06

int16 count;

int8 a;

//Chuong trinh ngat TMR0



#int_timer0

void interrupt_timer0()

{

set_timer0(6);



++count;

if(count == 2000) // 2000*500us = 500000us = 1s

{

count=0;


rotate_left(&a,1);

}

}



//Chuong trinh chinh

void main(void)

{

set_tris_b(0);



enable_interrupts(int_timer0);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);

enable_interrupts(global);

set_timer0(6);// T_dinhthi = 2*(256 - 6)*1us = 500us

a = 0x01;

while(true)

{

PORTB = a;



}

}



1.7. Nháy Led RB0

Chương trình này làm nhấp nháy con led ở chân RB0 1s sáng, 1s tắt.

Code:

#include<16F877A.h>



#fuses NOWDT,PUT,HS,NOPROTECT

#use delay(clock=10000000)

main()

{

while(true)



{

output_high(PIN_B0);

delay_ms(1000);

output_low(PIN_B0);

delay_ms(1000);

}

}



Trên đây:

Code:


#include<16F877A.h>

Khai báo con PIC bạn sử dụng, file này chương trình viết sẵn nhằm khai báo các bit, thanh ghi quan trọng trong con pic này.Các bạn có thể vào thư mục cài đặt C:\Program Files\PICC\Devices\16F877A.h để xem nó khai báo được những gì trong đó!

Code:

#fuses NOWDT,PUT,HS,NOPROTECT



Thiết lập các config

Code:


#use delay(clock=10000000)

Khai báo tần số dao động cấp cho PIC

Code:

output_high(PIN_B0)



Xuất ra chân RB0 mức logic 1 (tức 5V), do khi thực hiện hàm này đã bao hàm luôn việc tác động lên thanh ghi TRISB (dùng chọn chân I/O) do vậy ta không cần viết lệnh chọn chân I/O nữa.

Code:


output_low(PIN_B0)

Ngược lại

Code:

delay_ms(1000)



Tạo trễ khoảng thời gian theo mili giây là 1000 (tức 1s)
*Chú ý hàm này chỉ có tác dụng khi có khai báo tần số dao động cấp cho PIC
Và bây giờ thử làm cho tất cả 8 led nối với portB chớp tắt 1s xem nào!Phải chăng ta sẽ làm như sau (Viết trong vòng lặp while):

Code:


{

output_high(PIN_B0);

output_high(PIN_B1);

output_high(PIN_B2);

output_high(PIN_B3);

output_high(PIN_B4);

output_high(PIN_B5);

output_high(PIN_B6);

output_high(PIN_B7);

delay_ms(1000);

output_low(PIN_B0);

output_low(PIN_B1);

output_low(PIN_B2);

output_low(PIN_B3);

output_low(PIN_B4);

output_low(PIN_B5);

output_low(PIN_B6);

output_low(PIN_B7);

delay_ms(1000);

}

Viết như thế này thì quá dài và thiếu chính xác nữa, có cách nào khác hay hơn không ? Sao ta không xuất đẩy hẳn portB lên mức cao,tạo trễ 1s rồi ép cho nó xuống mức thấp,tạo trễ 1s cùng một lúc nhỉ !


Bài tiếp theo sẽ đưa ra câu trả lời....

output_high(pin_xx);


output_low (pin_xx);

Hai câu lệnh trên chỉ làm cho chân ra xx là cao hay thấp, ứng với mức logic 1 hoặc 0.trong bài trên ta muốn cho sáng tất một port thì chỉ càn câu lệnh :

Code:

void main (void )



{

set_tris_b(0); // cả port B là port ra

set_tris_c(0); // cả port C là port ra

port_b(0x00); // khởi tạo giá trị đầu port B là 0 ( Tất cả led đều tắt )

port_c(0x00); // khởi tạo giá trị đầu port B là 0 ( Tất cả led đều tắt )

delay_ms(100);

while(1)

{

port_b(0xff);



delay_ms(1000);

port_c(0xff);

delay_ms(1000);

}

}


1.8. Delay 1s portB

Ăc..ặc..đang post thì bị cúp điện,bực cả mình...


Và đây là câu trả lời cho việc delay led ở portB 1s

Code:


#include<16F877A.h>

#fuses NOWDT,PUT,HS,NOPROTECT

#use delay(clock=10000000)

#use fast_io(b)

#byte portb=0x6

main()


{

set_tris_b(0);

while(true)

{

portb=255; //all led on



delay_ms(1000);

portb=0; //all led off

delay_ms(1000);

}

}



Code:

#byte portb=0x6

Khai báo địa chỉ portB, không như trong MPLAB đã định nghĩa luôn cái này cho ta, nếu không có dòng này chương trình sẽ báo lỗi chưa định nghĩa portB

Code:


set_tris_b(0)

Tất cả các chân của portB là output, muốn set tất cả các chân là input thì trong ngoặc là 255,....Trong HELP hướng dẫn lệnh này như sau:


"These functions allow the I/O port direction (TRI-State) registers to be set. This must be used with FAST_IO and when I/O ports are accessed as memory such as when a #BYTE directive is used to access an I/O port. Using the default standard I/O the built in functions set the I/O direction automatically."
Rõ ràng khi set byte làm I/O nếu ta thêm khai báo:

Code:


#use fast_io(b)

Dùng khai báo này thì CCS sẽ chỉ thực hiện đúng một lệnh cho việc I/O các chân trên port tương ứng, nếu ko nó phải mất khoảng 3 hay 4 lệnh gì đó.


Phần I/O + Delay tạm thời xong như vậy, bác nào có phản hồi thì tiếp tục thảo luận.Xong phần này có thể viết về xuất led 7 đoạn, quét phím, LCD,....Bác nào viết thì cứ post lên cho bà con thảo luận nhé !

+Một phương án khác:
#include <16F877A.h>
#fuses HS, NOWDT, NOLVP, XT
#use delay(clock=4000000)
void main() {
byte leds = 0xff;
set_tris_b(0x00); // configure pins of PORTB as output
while (true) {
output_b(leds);
leds = ~leds; // toggle leds
delay_ms(1000);
}
}

1.9. Delay_Timer0

//*****************************************

// CHUONG TRINH SU DUNG TMR0

//date:23/08/2005

//author:noisepic@gmail.com

//status: OK!

//*****************************************

#include<16F877A.h>

#include

#fuses NOWDT,PUT,HS,NOPROTECT,NOLVP

#use delay(clock=20000000)
void tre_ms(unsigned int time);
void main()

{

TRISB=0;



setup_timer_0();

while(1)


{

PORTB = 0;

tre_ms(250);

PORTB =0xff;

tre_ms(250);

}

}



//******************************************

void tre_ms(unsigned int time)

{

int8 i,j;



GIE=0;

T0CS=0;// Chon internal

T0SE=0;// rising edge

PSA =0;// Timer mode

PS2=0;PS1=1;PS0=1; // 1:8

for(i=0;i

{

for(j=0;j<5;j++)



{

TMR0=132;

while(tmr0if==0);

tmr0if = 0;

}

}

}



//**********



cho em hỏi, em đang làm thí nghiệm pic16f84a, làm led chớp tắt theo ý muốn nhưng em muốn dùng 1 biến trở để chỉnh tốc độ delay thì phải làm sao? nhờ các anh chỉ giáo! và em muốn dùng time 0 được không?
(xin lỗi mấy anh, mục này mà em hỏi F84a)


Người ta thường dùng bộ ADC trong PIC (tất nhiên đang nói đến những chip có module ADC) để đọc giá trị của biến trở rồi điều chỉnh thời gian/tốc độ chớp tắt. Với PIC16F84A (không có module ADC), bạn vẫn có thể làm được điều đó, nhưng dùng một cách khác, được đề cập đến trong app. note 863 của Microchip. Bạn tìm đọc app. note đó trên web site của Microchip.

+cho em hoi trong ccs co ho tro vong lap " for" ko vay cac bac .. sao em lam hoai ma ko duoc vay .. bien dich van dung ma chay thi ko duoc...

Code:


#include<16F877.h>

#device *=16 ADC=8

#fuses NOWDT,PUT,HS,NOPROTECT

#use delay(clock=10000000)

#use fast_io(b)

#byte p3=0x06

#byte porta=0x05
void main()

{

int8 const led[]={1,2,4,8,16,32,64,128},a;



set_tris_b(0);

set_tris_a(0);

set_tris_c(0);

set_tris_d(0);

while(true)

{

for( a=0;a<8;a++)



p3=led[a];

delay_ms(100);

}

}

Như thế nào là chạy không được?


Theo code của bạn, trong vòng while quá trình sau sẽ được thực hiện, nếu a là biến:
1. Xuất 8 lần dữ liệu ra p3 (địa chỉ 0x06)
2. Làm trễ 100 ms
3. Quay lại bước 1

Em biet no sai o dau roi. Sau moi lan gia tri a tang len 1 . phai delay mot khoan thoi gian, neu ko no se chay den gia tri cuoi cung roi xuat ra portb , vay p3=led[7]=128; nhu vay xem nhu vong lap ko co tac dung(co vay thoi cugn so y thiet(~_~).. Vay doan code dung la 8 con led se chay duoi nhau hoai (^_^)..)

Code:

#include<16F877.h>



#device *=16 ADC=8

#fuses NOWDT,PUT,HS,NOPROTECT

#use delay(clock=10000000)

#use fast_io(b)

#byte p3=0x06
void main()

{

int8 const led[]={1,2,4,8,16,32,64,128},a;



set_tris_b(0);

while(true)

{

for(a=0;a<8;a++)



{

p3=led[a];

delay_ms(20);

}

}



}

namqn: bạn nên đặt phần code của bạn giữa hai tag [ code] và [ /code] để định dạng cho phần code (hai tag không có khoảng trắng nào hết chứ không phải như được hiển thị ở đây).



2. ADC

A. Sơ đồ:



B.Code

B.1. ADC reading voltage

--reading Voltage

#include <16f877A.h>

#include

#device *=16 ADC=10

#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, LVP, NOCPD, NOWRT

#use delay(clock=20000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)

#include


int16 temp,high,low;

int8 nghin,tram,chuc,donvi;

int1 mili_volt;

float volt;

int8 const a1[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; // Khong co cham

int8 const a2[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; // Co cham


#INT_EXT

void ngatngoai()

{
}

//==================================

void convert_bcd(int16 x)

{

nghin = x / 1000 + 0x30;



temp = x % 1000;

tram = temp / 100 + 0x30;

temp = temp % 100;

chuc = temp / 10 + 0x30;

donvi = temp % 10 + 0x30;

}

//===================================



void main() {
lcd_init();

printf(lcd_putchar,"CT Do dien ap");


setup_adc_ports(AN0);

chs0=0;chs1=0;chs2=0;

setup_adc(ADC_CLOCK_INTERNAL);

delay_us(10);


do {

temp = read_adc();

// Dang so thap phan

volt = (float)(temp*5)/1023;

if (volt < 1) mili_volt=1;

else mili_volt = 0;

// Dang so nguyen

high = (temp*5)/1023;

low = (temp*5)%1023;

// ====Truyen len may tinh

printf("\r\nGia tri ADC = %lu",read_adc());

Printf("\r\nGia tri dien ap = %f",volt);

//================

volt = volt * 1000;


convert_bcd((int16)volt);

lcd_putcmd(0xC0);

printf(lcd_putchar, "V = ");

if (!mili_volt) {lcd_putchar(nghin);lcd_putchar(".");}

lcd_putchar(tram);

lcd_putchar(chuc);

lcd_putchar(donvi);

if (mili_volt) printf(lcd_putchar," mV");

else printf(lcd_putchar," V");

printf("\r\n V = %lu",high);

if(low < 100)

printf(".0%2lu",low);

else

printf(".%lu",low);



} while(true);
}

B.2. LM335_LCD

a. Sơ đồ:



b. lcd_lib_4bit

#include


#define LCD_RS PIN_D2

//#define LCD_RW PIN_A1

#define LCD_EN PIN_D3
#define LCD_D4 PIN_D4

#define LCD_D5 PIN_D5

#define LCD_D6 PIN_D6

#define LCD_D7 PIN_D7


// misc display defines-

#define Line_1 0x80

#define Line_2 0xC0

#define Clear_Scr 0x01


// prototype statements

#separate void LCD_Init ( void );// ham khoi tao LCD

#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro

#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD

#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD

#separate void LCD_PulseEnable ( void );// Xung kich hoat

#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data

// D/n Cong

#use standard_io ( B )

#use standard_io (A)


//khoi tao LCD**********************************************

#separate void LCD_Init ( void )

{

LCD_SetData ( 0x00 );



delay_ms(200); /* wait enough time after Vdd rise >> 15ms */

output_low ( LCD_RS );// che do gui lenh

LCD_SetData ( 0x03 ); /* init with specific nibbles to start 4-bit mode */

LCD_PulseEnable();

LCD_PulseEnable();

LCD_PulseEnable();

LCD_SetData ( 0x02 ); /* set 4-bit interface */

LCD_PulseEnable(); /* send dual nibbles hereafter, MSN first */

LCD_PutCmd ( 0x2C ); /* function set (all lines, 5x7 characters) */

LCD_PutCmd ( 0b00001100); /* display ON, cursor off, no blink */

LCD_PutCmd ( 0x06 ); /* entry mode set, increment & scroll left */

LCD_PutCmd ( 0x01 ); /* clear display */

}

#separate void LCD_SetPosition ( unsigned int cX )



{

/* this subroutine works specifically for 4-bit Port A */

LCD_SetData ( swap ( cX ) | 0x08 );

LCD_PulseEnable();

LCD_SetData ( swap ( cX ) );

LCD_PulseEnable();

}

#separate void LCD_PutChar ( unsigned int cX )



{

/* this subroutine works specifically for 4-bit Port A */

output_high ( LCD_RS );

LCD_PutCmd( cX );

output_low ( LCD_RS );

}

#separate void LCD_PutCmd ( unsigned int cX )



{

/* this subroutine works specifically for 4-bit Port A */

LCD_SetData ( swap ( cX ) ); /* send high nibble */

LCD_PulseEnable();

LCD_SetData ( swap ( cX ) ); /* send low nibble */

LCD_PulseEnable();

}

#separate void LCD_PulseEnable ( void )



{

output_high ( LCD_EN );

delay_us ( 3 ); // was 10

output_low ( LCD_EN );

delay_ms ( 3 ); // was 5

}

#separate void LCD_SetData ( unsigned int cX )



{

output_bit ( LCD_D4, cX & 0x01 );

output_bit ( LCD_D5, cX & 0x02 );

output_bit ( LCD_D6, cX & 0x04 );

output_bit ( LCD_D7, cX & 0x08 );

}

B.3. LM335_F877A_LCD1602

/* Mach do nhiet do

- MCU = PIC16F877A

- Sensor = LM335 (co the thay the bang LM35D)

- MAX232 giao tiep may tinh

- LCD1602A de hien thi gia tri nhiet do

Mo ta phan cung:

- Mach cho sensor mac nhu trong Datasheet cua LM335

Chan V_out noi qua dien tro 1K voi +5V. Chan nay cung duoc noi voi kenh AN0 cua PIC

Chan Adj noi voi dien tro 10K de tinh chinh

Chan GND noi dat

- Mach VDK gom co LCD va max232

LCD noi voi PORTD cua PIC

RS -> RD2, RW -> GND, E -> RD3

D4-D7 -> RD4-RD7

Max232:

chan10 -> RC6, chan9 -> RC7



chan8 -> chan3 DB9, chan7 -> chan2 DB9, chan5 DB9 -> GND

Kenh AN0 cua PIC noi den chan V_out LM335

Nut bam noi tai chan RB0 -> nhan ngat ngoai

Thach anh loai 20MHz, tu 22pF

==============================================

- Designer: linhnc308@yahoo.com

- Chuc thanh cong cung VDK PIC

*/

#include <16F877A.h>



#include

#device *=16 adc=10

#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#use delay(clock=20000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)

#include

int8 low,high,key,mode,min,max,mode1,i;

int1 blink,on_off,blink_min,blink_max;

int1 do_F;

void convert_bcd(int8 x);

void set_blink();

void bao_dong();

void test();

//-----------------------------------------------------------------

void main()

{

float value;



on_off =1;

min =15; //nhiet do min default

max =35; //nhiet do max default

do_F =0 ;

i = 0 ;

mode =0 ;



mode1 = 0 ;

blink=0 ;


trisa = 0xFF;

trisb = 0x01;

trisd = 0x00;

printf("Chuong trinh do nhiet do\n");

LCD_init();

Printf(LCD_putchar,"Lop DT8 - BKHN");

LCD_putcmd(0xC0);

Printf(LCD_putchar,"Khoi tao...");

// Khoi tao cho ngat ngoai

enable_interrupts (INT_EXT);

ext_int_edge(H_TO_L);

enable_interrupts (GLOBAL);

// Khoi tao che do cho bo ADC

setup_adc_ports(AN0);

setup_adc(ADC_CLOCK_INTERNAL);

delay_us(10);

// Lay mau nhiet do lan dau tien

value=(float)read_adc();

value = (value - 558.5)/2.048; // For 5V supply

// value = (value - 754.8)/2.048; // For 3.7V Supply

// value = (value - 698.2)/2.048; // For 4V supply

convert_bcd((int8)value); // Chuyen doi tach so tram, chuc, donvi de hien thi len LED 7

delay_ms(1000);

LCD_putcmd(0xC0);

Printf(LCD_putchar," Init OK");
while(1)

{

if (i==15)



{

value = read_adc();

value=(value-558.5)/2.048;

if (do_F==1) value=1.8*value+32;

convert_bcd((int8)value);

printf("\n\rNhiet do phong: %u",value);

LCD_putcmd(0xC0);

printf(LCD_putchar," T = ");

LCD_putchar(high); LCD_putchar(low);

if (do_F==0) printf(LCD_putchar," C");

else printf(LCD_putchar," F");

i=0;


}

i++;


if(((int8)value > 40) || ((int8)value < 15)) on_off=1;

else


{

on_off = 0;

LCD_Putcmd(0xCF);

LCD_putchar(" ");

blink=0;

}

if (on_off==1)



{

if (blink==0) { LCD_Putcmd(0xCF);LCD_putchar("!");blink=1;delay_ms(250);}

else {LCD_Putcmd(0xCF);LCD_putchar(" ");blink=0;delay_ms(250);}

}

}



}

//end main-----------------------------------------------------------

#INT_EXT

void test()

{

if (do_F == 1) do_F=0;



else do_F=1;
}

void set_blink()

{

switch(mode)



{

case 1: blink_min=1; break;

case 2: {blink_max=1; blink_min=0;} break;

case 3: {mode=0; blink=0; blink_min=0; blink_max=0;} break;

}

}

void convert_bcd(int8 x)



{

low=x%10; //chia lay phan du, so hang don vi

high=x/10; //tach hang tram va hang chuc

low = low + 0x30;

high = high + 0x30;

}

void bao_dong(){



int8 i;
if (blink == 0) blink = 1;

else blink=0;


for(i=0;i<50;i++)

{

LCD_Putcmd(0xCF);



if (blink==0) LCD_putchar("!");

else LCD_putchar(" ");

}

}


Cho toi hoi tai sao khi khai bao:
#device PIC16F877 *=16 ADC=10
thi CCSC bao loi: "Can not change device type this far into the code"
Cach khac phuc

Đây là 1 ví dụ nhỏ về ADC,chân RA0 lấy tín hiệu Analog từ biến trở và xuất giá trị số biến đổi tương ứng qua tám led nối ở portB

Code:


#include <16F877.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP

#device 16F877*=16 ADC=8

#use delay(clock=10000000)

Int8 adc;

main()


{

setup_adc(adc_clock_internal);

setup_adc_ports(AN0);

set_adc_channel(0);

delay_ms(10);

while(true)

{

adc=read_adc();



output_B(adc);

}

}



Hình Kèm Theo

Khai báo:

Code:

"#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT



-HS :sử dụng thạch anh tần số cao
-NOWDT:tắt WDT
-NOPROTECT:tắt PROTECT
-NOLVP:không dùng LVP
-NOBROWNOUT:ko BROWNOUT
Còn cụ thể ý nghĩa thế nào,bạn vào help của CCS C gõ :"#fuses"

Cái luồng bên này nhh vẽ hình cẩu thả quá!


Cái chân MCLR*, bạn phải nối thên cái công tắc ấn vào. nhh nối như vậy thì không reset được con PIC đâu
Hai cái chân Vss phải được nối mass.
Chân nào không xài, nhh nên đánh dấu bỏ đi (trong thanh công cụ của ORCAD có cái dấu này đó).

Một cách viết khác để tham khảo:
#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP
#device 16F877*=16, ADC=8
#use delay(clock=4000000)

void main() {


setup_adc(adc_clock_internal);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0); // TM Board: VR3=0, VR2=1, VR1=2
delay_ms(10);
while (true)
output_b(read_adc());
}

B.4. ADC_186

// ANALOGUE TO DIGITAL CONVERTER HARDWARE INTERFACE PROGRAM

// COPYRIGHT PROPERTY OF ALPHADATA DESIGNS LIMITED (c) 1999

// WRITTEN FOR THE MAX186 12 BIT 8-CHANNEL MICRO-WIRE A TO D


// published by permission of Alphadata designs on

// Hi-Tech C website, http://www.workingtex.com/htpic. Thanks!

//------------------------------------------------------

// Version History

//------------------------------------------------------

// Issue 1.0 : 21/12/1999 : First Officially Released

//------------------------------------------------------

#include "ioh8314.h"

#include "h8genlib.h"

#include "spi.h"
extern byte p4dr;
/* call this to set up the hardware ports */
void adc_initialise(void)

{

}



/* this table makes up the muddled way the device is addressed */

/* see the data sheet for the MAX186 to see why ! */

const byte command_table[]={ 0b10001111,

0b11001111,

0b10011111,

0b11011111,

0b10101111,

0b11101111,

0b10111111,

0b11111111};

/* call this to read one channel of data from the a to d */

word adc_read_channel(byte channel_number)

{

byte bits;



byte command;

int data;


spi_adc_select();

/* Start by sending command byte */

command = command_table[channel_number];
for (bits=0;bits<8;bits++) {

if ((command & 0x80) == 0) spi_lodata(); else spi_hidata();

command = command << 1;
spi_hiclock();

spi_loclock();

}

spi_lodata();



spi_hiclock();

spi_loclock();


data = 0;

for (bits=0;bits<12;bits++) {

data = (data << 1);

if (P4DR & 0b10000000) data = data | 0x0001;

spi_hiclock();

spi_loclock();

}

spi_adc_deselect();



return (data);

}

// does an average reading



word adc_filtered_read(byte channel,int readings)

{

long av;



word lp;

av = 0;


for (lp=0;lpav = av + adc_read_channel(channel);

}

return (word)(av / readings);



}

// end



tải về 1.15 Mb.

Chia sẻ với bạn bè của bạn:
1   2   3   4   5   6   7   8   9   ...   15




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương