-- TEMPO detection program -- Display on a 2x16 LCD the colour of the "Tempo" day (BLUE, WHITE or RED) -- and the expired & remaining days when WHITE or RED -- two buttons on A0 & A1 allow for ajustment of the number of remaining days -- a short push to display / a long push to adjust -- those 2 figures are stored in EEPROM after being changed -- -- Compiled with JALV2 -- -- include 16f690 -- target PICmicro -- -- Basic fuses settings -- pragma target clock 8_000_000 ; oscillator running at 8MHz pragma target OSC INTOSC_NOCLKOUT ; internal oscillator used pragma target WDT disabled ; no watchdog pragma target MCLR external ; reset externally -- enable_digital_io() ; make all pins digital I/O OSCCON = 112 JANSEL_ANS8 = on -- pin_C6/AN8 is analog input ADCON0_CHS = 0x08 -- select ADC channel 08 (AN8) ADCON0_ADON = ENABLED -- enable ADC module ADCON1_ADCS = 0x02 -- A/D conv. clock to Fosc/32 (4us at 8MHz) include delay const byte LCD_ROWS = 2 -- LCD with 2 lines const byte LCD_CHARS = 16 -- and 16 characters per line const byte treshold1 = 15 const byte treshold2 = 15 const word target = 62700 const byte maxjrouge = 22 const byte maxjblanc = 43 -- -- -- var bit first_signal var byte vect[41],last29,nbtrame,nbtrame_start,returnsub,index var byte njblanc,njrouge,jour_save,i1 var sword cumul[4] -- alias lcd_en is pin_C4 -- data trigger alias lcd_rs is pin_C5 -- command/data select. alias boutonblanc is pin_A0 pin_A0_direction = input alias boutonrouge is pin_A1 pin_A1_direction = input include pic_data_eeprom pin_C4_direction = output pin_C5_direction = output alias lcd_dataport is portC_low -- 4 databits portC_low_direction = all_output data_eeprom_read(0, njblanc) if njblanc > maxjblanc then njblanc = 0 end if data_eeprom_read(1, njrouge) if njrouge > maxjrouge then njrouge = 0 end if for 41 using index loop vect[index]=0 end loop first_signal = true -- First tempo signal flag vect[29] = 1 -- Initialize "Demain" to Unknown vect[26] = 1 -- Initialize "Tarif" to "HP" / comment out for "HC" -- include lcd_hd44780_4 lcd_init() include format include print delay_100ms(5) -- -- function abs16(sword in x) return sword is if x < 0 then return sword(-x) else return sword(x) end if end function -- -- Detect the 175Hz signal -- function detect175Hz() return byte is for 4 using index loop cumul[index]=0 end loop index = 0 for 28 loop -- 28 samples over a 40ms period T1CON_TMR1ON = 0 -- stop Timer1 TMR1 = target -- initialize Timer1 with target PIR1_TMR1IF = 0 -- reset TMR1 overflow flag to zero T1CON_TMR1ON = 1 -- start Timer1 ADCON0_GO = TRUE -- start ADC conversion while (ADCON0_NDONE == TRUE) loop -- simply wait for conversion .. end loop cumul[index]=cumul[index]+ADRESH index = index + 1 if index > 3 then index = 0 end if while ! PIR1_TMR1IF loop -- wait for TMR1 overflow end loop -- each (65535-target)/2 ms end loop cumul[0]=abs16(cumul[0]-cumul[2])+abs16(cumul[1]-cumul[3]) if (cumul[0] >= treshold1) then return 1 else return 0 end if end function -- -- -- function testbit(byte in ntime) return byte is nbtrame = 0 for ntime loop nbtrame = nbtrame + detect175Hz() end loop return nbtrame end function -- -- -- procedure affiche_jour is if vect[36] then print_string(lcd,"BLANC") elsif vect[39] then print_string(lcd,"ROUGE") else print_string(lcd,"BLEU ") end if end procedure -- -- -- procedure affiche_tarif is if vect[26] | vect[35] then print_string(lcd,"HP") else print_string(lcd,"HC") end if end procedure -- -- -- procedure affiche_demain is if vect[21] then print_string(lcd,"blanc") elsif vect[24]then print_string(lcd,"rouge") elsif vect[29]then print_string(lcd,"???? ") else print_string(lcd,"bleu ") end if end procedure --- --- --- procedure affiche_tempo is lcd_cursor_position(0,1) affiche_jour print_string(lcd," ") affiche_tarif print_string(lcd," ") if vect[36] then print_byte_dec(lcd,njblanc/10) print_byte_dec(lcd,njblanc % 10) print_string(lcd, "/") print_byte_dec(lcd,(maxjblanc- njblanc)/10) print_byte_dec(lcd,(maxjblanc - njblanc) % 10) elsif vect[39] then print_byte_dec(lcd,njrouge/10) print_byte_dec(lcd,njrouge % 10) print_string(lcd, "/") print_byte_dec(lcd,(maxjrouge - njrouge)/10) print_byte_dec(lcd,(maxjrouge - njrouge) % 10) else print_string(lcd," ") end if lcd_cursor_position(1,1) print_string(lcd,"demain : ") affiche_demain end procedure -- -- -- procedure check_boutonblanc is var byte buttoncount If boutonblanc Then delay_1ms(10) -- Debounce time buttoncount = 0 While boutonblanc loop delay_1ms(10) buttoncount = buttoncount + 1 end loop If buttoncount > 30 Then -- Long push: adjust then display on LCD njblanc = njblanc + 1 If njblanc > maxjblanc then njblanc = 0 End if data_eeprom_write(0, njblanc) End if -- Short push: display only jour_save = vect[36] vect[36] = 1 affiche_tempo -- display of changes done through White button delay_100ms(10) vect[36] = jour_save affiche_tempo -- Restore initial state End if end procedure -- -- -- procedure check_boutonrouge is var byte buttoncount If boutonrouge Then delay_1ms(10) -- Debounce time buttoncount = 0 While boutonrouge loop delay_1ms(10) buttoncount = buttoncount + 1 end loop If buttoncount > 30 Then -- Long push: adjust then display on LCD njrouge = njrouge + 1 If njrouge > maxjrouge then njrouge = 0 End if data_eeprom_write(1, njrouge) End if -- Short push: display only jour_save = vect[39] vect[39] = 1 affiche_tempo -- display of changes done through Red button delay_100ms(10) vect[39] = jour_save affiche_tempo -- Restore initial state End if end procedure -- -- -- procedure startbit is -- infinite loop until forever loop -- 175Hz signal is detected if detect175Hz() then -- If 175Hz signal detected ... nbtrame = 1 + testbit(24) -- ... complete startbit acquisition nbtrame_start = nbtrame returnsub = 1 if (nbtrame > treshold2) then -- if startbit acquisition ok ... returnsub = 2 exit loop -- ... exit startbit end if end if check_boutonblanc check_boutonrouge end loop end procedure -- -- Decodage des trames -- procedure decode_frame is -- read Pulsadis 40 bits (1 s) signal delay_100ms(12) -- wait 1248 ms (1250 ms in theory) delay_1ms(48) -- (1250ms+1500ms=2750ms) i1 = 0 for 40 loop -- start signals acquisition delay_100ms(15) -- wait 1500 ms between Pulsadis bits i1 = i1 + 1 vect[i1] = testbit(25) -- store in vect[i] intensity of end loop -- the i th bit signal (1 <= i <=40) returnsub = 3 -- intensity is comprised between end procedure -- O (no signal) & 25 (max signal) -- -- -- procedure process_frame is i1 = 0 for 40 loop i1 = i1 + 1 if vect[i1] > treshold2 then -- Is signal strong enough ... vect[i1] = 1 -- then considered OK else vect[i1] = 0 end if end loop if first_signal then -- First signal: do not update njblanc/njrouge first_signal = false else if vect[36] & vect[29] & (!last29) then -- If new "BLANC" day ... njblanc = njblanc + 1 -- ... update counter ... if njblanc > maxjblanc then njblanc = 0 end if data_eeprom_write(0, njblanc) -- ... and save to eeprom 0 end if if vect[39] & vect[29] & (!last29) then -- If new "ROUGE" day ... njrouge = njrouge + 1 -- ... update counter ... if njrouge > maxjrouge then njrouge = 0 end if data_eeprom_write(1, njrouge) -- ... and save to eeprom 1 end if end if last29 = vect[29] -- Save vect[29] (Demain) for next signal affiche_tempo -- Display results on LCD returnsub = 1 end procedure -- -- MAIN PROGRAM -- affiche_tempo -- Display initial values on LCD returnsub = 1 forever loop -- Endless loop case returnsub of 1: startbit 2: decode_frame 3: process_frame end case end loop