void main() { SET_TRIS_A(0b00000001); SETUP_ADC_PORTS(AN0); SETUP_ADC(ADC_CLOCK_INTERNAL); ENABLE_INTERRUPTS(GLOBAL | INT_TIMER0); SETUP_TIMER_0(RTCC_INTERNAL | RTCC_8_BIT| RTCC_DIV_16); SET_TIMER0(100); while(true) { SET_ADC_CHANNEL(0); //Configura o canal de leitura 0 delay_us(100); //Tempo de ajuste do canal (necessário) ad0 = READ_ADC(); //Faz a conversão AD e a salva na variável ad0 ad0 = (ad0 * 5000)/1023; d1 = ad0/1000; d2 = (ad0/100) % 10; d3 = (ad0/10) % 10; delay_ms(500); } }
void main(){ OUTPUT_LOW(LCD_RW); //Che do ghi LCD_Init(); //Khoi tao LCD LCD_PutCmd(0x01); //Xoa man hinh ENABLE_INTERRUPTS(INT_TIMER0); //Kich hoat ngat ngoai SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_32); //Xung kich noi va chia truoc 32 ENABLE_INTERRUPTS(GLOBAL); //Cho phep ngat toan cuc SET_TIMER0(100); //Bat dau dem tu 100, khi tran Timer0 duoc 1ms while (True){ //Duy tri hoat dong cua vi dieu khien if (dem > N_max){ dem = 0; LCD_PutCmd(0x01); } LCD_SetPosition(0x00); //Cot 1 dong 1 LCD_PutChar("Dem so:"); LCD_SetPosition(0x07); //Cot 8 dong 1 printf(LCD_PutChar,"%lu",dem); } }
void main () { OUTPUT_B(0x00); // Apaga todos os leds da porta D /* Configura o Timer 0 para contagem interna e prescaler de 1:256 */ SETUP_TIMER_0(RTCC_EXT_H_TO_L | RTCC_DIV_2); /* Inicializa Timer 0 com o valor 61 */ SET_TIMER0(250); while(true) //Loop principal { if (T0IF) //Se o bit T0IF for igual a 1 { T0IF = 0; //Faz o bit igual a 0 SET_TIMER0(250); //Retoma o valor inicial do Timer 0 OUTPUT_B(leds=~leds); //Inverte a situação dos Leds } } }
void main(){ TRISE = 0x00; //Chan OUTPUT ENABLE_INTERRUPTS(INT_TIMER0); //Kich hoat ngat tran Timer0 SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_32); //Xung kich noi va chia truoc 32 ENABLE_INTERRUPTS(GLOBAL); //Cho phep ngat toan cuc SET_TIMER0(100); //Bat dau dem tu 100, khi tran Timer0 duoc 1ms while (True){ //Duy tri hoat dong cua vi dieu khien ; } }
void Ngat_Timer0(){ SET_TIMER0(100); //Xu ly chuong trinh ngat ++count; //Tang gia tri bien dem if (count == 1000){ CLEAR_INTERRUPT(INT_TIMER0); //Xoa co ngat DISABLE_INTERRUPTS(GLOBAL); //vo hieu hoa ngat toan cuc count = 0; PORTE = ~PORTE; ENABLE_INTERRUPTS(GLOBAL); //Cho phep ngat toan cuc } }
void Ngat_Timer0(){ SET_TIMER0(100); //Xu ly chuong trinh ngat count++; //Tang gia tri bien dem if (count == sleep){ CLEAR_INTERRUPT(INT_TIMER0); //Xoa co ngat DISABLE_INTERRUPTS(GLOBAL); //vo hieu hoa ngat toan cuc count = 0; dem++; //Tang gia tri cua bien dem len ENABLE_INTERRUPTS(GLOBAL); //Cho phep ngat toan cuc } }
void trata_t0() { conta++; if (conta>3) conta = 1; switch(conta) { case 1: OUTPUT_B(digito[d1] | 0b10000000); OUTPUT_A(0b00000000); OUTPUT_C(0b00000001); break; case 2: OUTPUT_B(digito[d2]); OUTPUT_C(0b00000010); break; case 3: OUTPUT_B(digito[d3]); OUTPUT_C(0b00000000); OUTPUT_A(0b00100000); break; } SET_TIMER0(100); }