Project mẫu PIC (phần 2)

19/03/2012 0 3340

15.Chương trình nhận dữ liệu từ cổng nối tiếp và hiển thị ra portC có tốc độ Baud là 9600.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#device  *=16 ADC = 10 
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz
#use     rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7)      //khoi tao usart

void main() 

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);         
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   // TODO: USER CODE!!  
   trisc = 0x00;                    //PORTC xuat du lieu.
   while(1) 
   {  
    if (kbhit())                    //Cho data den.
       portc = getch();             //Nhan data va xuat data ra PORTC.
   } 
}

16.Chương trình đọc dữ liệu từ port D và gửi về cổng nối tiếp có tốc độ baud là 4800.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

 

#include "16f887.h"
#include "def_16f887.h"
#device  *=16 ADC = 10 
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz
#use     rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7)      //khoi tao usart

 

void main() 

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);         
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   // TODO: USER CODE!!  
   trisc = 0x10;                    //PORTC xuat du lieu.
   while(1) 
   {  
      putc('A');                    //Gui ky tu A
      delay_ms(500);
   } 
}

 

17.Chương trình đọc dữ liệu tín hiệu analog từ chân AN0 và gửi về cổng nối tiếp có tốc độ Baud là 9600.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

 

#include "16f887.h"
#include "def_16f887.h"
#device  *=16 ADC = 10 
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)                                           //Thach anh 4MHz
#use     rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7)      //khoi tao usart
int16 data;
unsigned char dataL,dataH;
void main() 

   setup_spi(SPI_SS_DISABLED); 
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 
   setup_timer_2(T2_DISABLED,0,1); 
   setup_comparator(NC_NC_NC_NC); 
   setup_vref(FALSE);     
   setup_timer_1(T1_INTERNAL);  
   setup_adc_ports(sAN0|VREF_VREF);  //kenh A0 nhan tin hieu, A2 = 0V; A3 = 5V; 
   setup_adc(ADC_CLOCK_INTERNAL);    //Thoi gian lay mau bang clock he thong. 
   delay_ms(5);                      //Cho thiet lap xong ADC.
   // TODO: USER CODE!! 
    
   trisa = 0xff;                     //PORTA nhan du lieu.
   trisc = 0x10;                    
   set_adc_channel(0);               //Chon kenh 0.
   delay_us(10);                     //cho chon kenh xong. 
     
   while(1) 
   {  
      data = read_adc();            //doc gia tri analog da chuyen doi sang so.
      data = data/10.24;            //stepsize = 50mV. Neu giao tiep LM35 thi chia 2.048 (stepsize = 10mv)
      dataL = data;                 //Lay byte thap.
      dataH = data>>8;              //Lay byte cao.
      putc(dataL);                  //Gui byte thap.
      putc(dataH);                  //Gui byte cao.
      delay_ms(500);
   } 
       
}

 

18.Đọc dữ liệu portB xuất ra portD khi có thay đổi từ RB4 – RB7.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz

#int_RB
void  RB_isr(void) 
{
   PORTD = PORTB;
   delay_ms(20);
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);            
   enable_interrupts(INT_RB);
   enable_interrupts(GLOBAL);

   set_tris_b(0xff);       //PORTB xuat du lieu.
   port_b_pullups(0xff);   //Kich hoat dien tro treo.
   trisd = 0x00;           //PORTD xuat data.
   while(1);               
}

19.Đếm nhị phân và reset mạch đếm khi có thay đổi RB4 - RB7.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz

unsigned char number=0;

#int_rb
void  rb_isr(void) 
{
   number = 0;                   //reset so dem
   PORTC = 0x00;                 //cac led tat
   delay_ms(50);
   RBIF=0;
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);            
   enable_interrupts(int_rb);
   enable_interrupts(GLOBAL);

   set_tris_b(0xff);           //PORTB nhan data.
   set_tris_c(0x00);           //PORTC xuat data.
   
   while(1)
   {
      number++;                  //so dem.
      PORTC = number;            //xuat so dem ra PORTC.
      delay_ms(500);             //Toc do dem 0.5s
   }
}

 

20.Đếm nhị phân và reset mạch đếm khi RB0 chuyển từ cao xuống thấp.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz

unsigned char number=0;

#int_ext
void  EXT_isr(void) 
{
   number = 0;
   PORTC = 0x00;
   delay_ms(20);
}

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);   // This device COMP currently not supported by the PICWizard
   enable_interrupts(int_ext);
   ext_int_edge(H_to_L);            //Ngat khi co canh xuong.   
   enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
   set_tris_b(0x01);       //RB0 nhan du lieu.
   port_b_pullups(1);
   set_tris_c(0x00);       //PORTC xuat du lieu.
   while(1)
   {
      number++;
      PORTC = number;
      delay_ms(500);
   }

}

21.Đọc dữ liệu từ portB xuất ra portD khi có sự thay đổi từ bit RB4 - RB7.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 4000000)        //Thach anh 4MHz

unsigned char number=0;

#int_rb
void  rb_isr(void) 
{
   number = 0;                   //reset so dem
   PORTC = 0x00;                 //cac led tat
}

void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   
   clear_interrupt(int_rb); 
   enable_interrupts(int_rb);
   enable_interrupts(GLOBAL);
   
   set_tris_b(0xff);           //PORTB nhan data.
   set_tris_c(0x00);           //PORTC xuat data.
   
   while(1)
   {
      if(RB0)
       {
         number++;             //so dem.
         PORTC = number;       //xuat so dem ra PORTC.
       }
      else
         PORTC = 0x0f;
      delay_ms(500);           //Toc do dem 0.5s
   }
}

22.Bộ định thời tạo xung vuông chu kì 10ms, duty cycle 50% tại RD0.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 8000000)        //Thach anh 8MHz

#int_TIMER1
void  TIMER1_isr(void) 
{
   RD0 = !RD0;                               //Tao xung.
   set_timer1(55535);
}

void main()
{
//Khoi tao T1 va ngat.
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);  //T1 dem xung noi, ti le chia 1
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer1(55535);         //CK xung = 10ms, he so duty 50% => muc cao = muc thap = 5ms
                              //Thach anh 8 MHz => CK may = 4/8 = 0,5us
                              //5ms = 5000us = 10.000xung = 65535 - 55535
//Thiet lap cac pin xuat nhap
   set_tris_d(0x00);          //PORTD xuat tin du lieu.
//----------------------------
   while(1);                  //VDK ko lam gi.  
}

23.Bộ định thời tạo xung vuông có tần số 1KHz, duty cycle 50% tại RD0.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

 

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 8000000)        //Thach anh 8MHz

#int_TIMER1
void  TIMER1_isr(void) 
{
   RD0 = !RD0;                               //Tao xung.
   set_timer1(64535);
}

void main()
{
//Khoi tao T1 va ngat.
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);  //T1 dem xung noi, ti le chia 1
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer1(64535);         //f = 1KHz => CK xung = 1ms, he so duty 50% => muc cao = muc thap = 0,5ms
                              //Thach anh 8 MHz => CK may = 4/8 = 0,5us
                              //0,5ms = 500us = 1000xung = 65535 - 64535
//Thiet lap cac pin xuat nhap
   set_tris_d(0x00);          //PORTD xuat tin du lieu.
//----------------------------
   while(1);                  //VDK ko lam gi.  
}

24.Dùng ngắt và Timer để tạo xung vuông có tần số 2KHz tại RD0. Đọc dữ liệu từ portB và xuất ra portC.

  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 8000000)        //Thach anh 8MHz

#int_TIMER1
void  TIMER1_isr(void) 
{
   RD0 = !RD0;                               //Tao xung.
   set_timer1(65035);
}

void main()
{
//Khoi tao T1 va ngat.
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);  //T1 dem xung noi, ti le chia 1
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer1(65035);         //f = 2KHz => CK xung = 0,5ms, he so duty 50% => muc cao = muc thap = 0,25ms
                              //Thach anh 8 MHz => CK may = 4/8 = 0,5us
                              //0,25ms = 250us = 500xung = 65535 - 65035
//Thiet lap cac pin xuat nhap
   set_tris_b(0xff);          //PORTB nhap du lieu.
   port_b_pullups(0xff);      //Kich hoat dien tro treo portb.
   set_tris_c(0x00);          //PORTC xuat du lieu.
   set_tris_d(0x00);          //PORTD xuat du lieu.
//----------------------------
   while(1)
   {
     PORTC = PORTB;
     delay_ms(20);
   }
}

25.Bộ định thời tạo xung vuông có tần số 1KHz, duty 50% tại RD0 và xung vuông tần số 2KHz, 50% duty
  • Sơ đồ mạch điện:
  • Chương trình mẫu:

#include "16f887.h"
#include "def_16f887.h"
#fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
#use     delay(Clock = 8000000)        //Thach anh 8MHz

#int_TIMER1
void  TIMER1_isr(void) 
{
   RD1 = !RD1;                 //Tao xung 2KHz.
      if(RD1==0)               //RD1 = 0 thi dao trang thai RD0.  
         RD0 = !RD0;           //Tao xung 1KHz.                    
   set_timer1(65035);
}

void main()
{
//Khoi tao T1 va ngat.
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);  //T1 dem xung noi, ti le chia 1
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer1(65035);         //f = 2KHz => CK xung = 0,5ms, he so duty 50% => muc cao = muc thap = 0,25ms
                              //Thach anh 8 MHz => CK may = 4/8 = 0,5us
                              //0,25ms = 250us = 500xung = 65535 - 65035
//Thiet lap cac pin xuat nhap
   set_tris_d(0x00);          //PORTD xuat du lieu.
//----------------------------
   while(1);
}

Nguồn: sangthai.com.vn

Đăng nhập

Chat