Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
// Tao delay_timer_01
// Tao delay_timer_02
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
// vay 10ms dem duoc 11250(so)
while(1)
{
if(Count1<50)
{
output_high(PIN_C0);
}
else
{
output_low(PIN_C0);
if (Count1>100)
{
Count1=0;
}
}
}
}
// Cach Tinh Timer MAX
// Xung thach anh 20.000.000 Hz ==> xung noi 5.000.000 hz
// Thoi gian 1 chu Ky` = 1/xung noi = 1/5000.000 = 0.2 us
// NEU TA DAT (setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);) thi ta co
// With an internal clock at 20mhz
// and with the T1_DIV_BY_1 mode, the timer
// will increment every 0.2us. It will overflow every 40 ms
// NEU TA DAT (setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);) thi ta co
// With an internal clock at 20mhz
// and with the T1_DIV_BY_1 mode, the timer
// will increment every 0.2*8=1.6us. It will overflow every 104.8576ms
// Voi lua chon nhu sau thi ta co cac Timemax nhu sau:
//T1_DIV_BY_1
: timemax=13.1ms
//T1_DIV_BY_2
: timemax=26.2ms
//T1_DIV_BY_4
: timemax=52.4ms
//T1_DIV_BY_8
: timemax=104.8ms
// So de set Timer1 duoc tinh nhu sau: set_timer1(Bien);
//T1_DIV_BY_1
: Bien = 65536-(samplingtime (s)/(4/20000000))
//T1_DIV_BY_2
: Bien = 65536-(samplingtime (s)/(8/20000000))
//T1_DIV_BY_4
: Bien = 65536-(samplingtime (s)/(16/20000000))
//T1_DIV_BY_8
: Bien = 65536-(samplingtime(s)/(32/20000000))
// Tao delay_timer_01
// Tao delay_timer_02
{
Count1++;
Count2++;
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
}
// vay 10ms dem duoc 11250(so)
//******* Bat dau chuong trinh *************
main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
if(!input(PIN_A4))
// SW is ON
{
output_low (PIN_C2); // Tat LED C2
CHOP_LED();
// goi Ham CHOP_LED (Signal to known system is ON)
}
else
// SW is OFF
{
output_low(PIN_C0); // Tat LED C0
output_high(PIN_C2); // Sang LED C2 (Signal to known system is OFF)
}
}
}
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);
while(1)
{
if(!input(PIN_A4)) // Read errors
{
delay_ms(200);
READ_SO();
BIN2BCD(Doc_so);
delay_ms(200);
output_c(Doc_so);
}
else
{
BIN2BCD(1200);
delay_ms(15);
output_c(0);
}
}
}
// Tao delay_timer_01
// Tao delay_timer_02
// Ttao delay tre giua chuong trinh
// Vi du 6
// Ung dung ==> neu cong tac ON ==> sang va toi LED port C
//
neu cong tac OFF ==> Sang LED
// Date: May 08, 2007
// Designed by Ha Ngoc Nguyen
//*********************
// PIN RC0 ~RC5 noi voi den LED
// PIN RA4 lam Switch
//*********************
#include <18F452.h>
//#include <16F877A.h>
//#device ADC=16
//#device ADC=8
//#include <math.h>
//#include <string.h>
//#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
int16 Count1=0;
int16 Count2=0;
int16 Doc_so=0;
// Tao delay_timer_01
// Tao delay_timer_02
delay_us(10);
Doc_so=read_adc()/2; // so doc duoc tu A/D
}
//******* chuong trinh Ngat Timer 1 ************
#INT_TIMER1
void Sampling_Time()
{
Count1++;
Count2++;
set_timer1(54286);
// dem 65535(so) mat thoi gian 52.4 ms
}
// vay 10ms dem duoc 11250(so)
//******* Bat dau chuong trinh *************
main()
{
output_C(255); // bat sang toan bo Led port C
delay_ms(1000);
output_C(0); // tat toan bo Led Cong C
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
//**Timmer max = 65536*4*4/OSC=65536*4/20000000=52.4ms**
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);
while(1)
{
if(!input(PIN_A4))
// SW is ON
{
READ_SO();
output_low(PIN_C2); // Tat LED C2
output_high(PIN_C7);
CHOP_LED();
// goi Ham CHOP_LED (Signal to known system is ON)
}
else
// SW is OFF
{
output_low(PIN_C0); // Tat LED C0
output_high(PIN_C2); // Sang LED C2 (Signal to known system is OFF)
output_low(PIN_C7);
}
}
}
void main()
{
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_INTERNAL);