View Single Post
Old 21-09-2006, 12:44 PM   #294
falleaf
PIC Bang chủ
 
falleaf's Avatar
 
Tham gia ngày: May 2005
Bài gửi: 2,631
:
Send a message via Yahoo to falleaf
picvietnam.c

Code:
#include <16F877A.h>
#fuses NOWDT,NOLVP,HS
#use delay (clock=20000000) //20Mhz

#use rs232(baud=2400, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
#include <7Segments_Led_Display.c>
#include <init_timer0.c>

#byte TMR2 = 0x11
#byte PR2 = 0x92
#byte CCPR1L = 0x15
#byte CCPR1H = 0x16
#byte CCP1CON = 0x17
int16 i = 0;
int16 j = 0;
int16 counted_round_value = 0x00;
int16 update_counted_round_value = 0x00;


void convert_16bits_to_decimal_digit(int16 counted_round_value1) // calculate to display on 7_segment leg Max value 9999 or 0x270F
{
   fourth_digit = counted_round_value1 / 1000;
   third_digit = (counted_round_value1 % 1000) / 100;
   second_digit = ((counted_round_value1 % 1000) % 100) / 10;
   first_digit = ((counted_round_value1 % 1000) % 100) % 10;
}


#INT_TIMER0 // ngat timer0 tang bien dem len 1
void timer0_int()
{
      counted_round_value++;

}

#INT_TIMER1 // ngat timer0 tang bien len 1
void timer1_int() // moi lan timer1 tran la 0.02s
{
   TMR1_L = START_VALUE_TIMER1_L;
   TMR1_H = START_VALUE_TIMER1_H;


   if (j == 5){ // chu ki lay mau 0.02 * 5 = 0.1s
      j = 0;

      update_counted_round_value = counted_round_value*500 + get_timer0(); // so vong quay trong 1 phut

      counted_round_value = 0;

      printf("%ld\t",update_counted_round_value); // gui so xung cap nhat ve may tinh qua RS232
   }
   else

      j++;
}



void init_PWM(int16 frequency){

   setup_ccp1(CCP_PWM); // initiate PWM
   PR2 = 20000000/4/frequency - 1; // set PWM period

   setup_timer_2(T2_DIV_BY_1,PR2,1);   // initiate time 2  The cycle time will be (1/clock)*4*t2div*(period+1)
                                       //   (1/20000000)*4*1*(255+1) =  51.2 us( will over flow every 51.2 us, will intrup every 51.2 uS or 19.5 khz;

}

void main()
{
   int8 timer0_value;

   set_tris_a(0xff);

   init_PWM(19500);
   init_timer0();

   set_pwm1_duty(500); //set the duty cycle = 500*1/clock* T2div (T2div is in setup_timer2)

                                                 //= 500/20000000*1 = 25uS (~25%)

      while(true){

   convert_16bits_to_decimal_digit(update_counted_round_value); // tinh ra tung digit de hien thi
   display_RPM_digit(); // hien thi tren led 7 doan
   }
}
falleaf vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn