CCP module on PIC controller? - Page 1
Forum section: electronics / general

author: conducteur
author_rank: Regular Contributor
timestamp: April 18, 2015, 07:55:28 pm
content: I want to decode the DCC protocol used for model railroading with a pic microcontroller. I start this project with an experiment: a 15khz square wave is connected to the CCP2 input pin. The CCP module should capture the values of TMR1 on each rising edge and display that value on the lcd.I know the lcd is connected an configured correctly, but i don't see a value appearing on it. I think the interrupt is not fired and it's likely that i forgot to enable something, but can't find what that might be.Code:;-------------------------------------------------------------------------------;programma;Controller = PIC 16F886;-------------------------------------------------------------------------------include 16f886;Alle mogelijke 'fuses' voor een 16F887;8MHz internpragma target OSC INTOSC_CLKOUTpragma target clock 8_000_000 -- oscillator frequencypragma target WDT disabled -- WDT offpragma target LVP disabled -- no low voltage programmingpragma target PWRTE DISABLED -- power-up timer enable bitpragma target MCLR Internal -- re3/mclr pin function select bitpragma target CPD DISABLED -- data code protection bitpragma target BROWNOUT DISABLED -- brown out reset selection bitspragma target IESO DISABLED -- internal external switchover bitpragma target FCMEN DISABLED -- fail-safe clock monitor enabled bitpragma target DEBUG DISABLED -- in-circuit debugger mode bitOSCCON_IRCF=0b111 -- set oscillator to full speed 8 Mhz (standard 4 Mhz)-------------------------------------------------------------------------------;lcd setup-------------------------------------------------------------------------------const byte LCD_ROWS = 2 --LCD with 2 linesconst byte LCD_CHARS = 16 --and 16 characters per linealias lcd_en is pin_b5 --LCD ENalias lcd_rs is pin_b4 --LCD RSpin_b5_direction = output --Beidenpin_b4_direction = output --als uitgangenalias lcd_dataport is porta_low --4 databitsporta_low_direction = output --eveneens als uitgang;De bibliotheek met LCD routines ladeninclude lcd_hd44780_4;Gebruik maken van bestaande routines om iets op het LCD tre laten verschijneninclude print;Getallen geformatteerd weergeven op het LCD met ingebouwde functies uit de;format bibliotheekinclude format;En het LCDinitialiserenlcd_init()--------------------------------------------------------------------------------;setup registers for capture on ccp2--------------------------------------------------------------------------------INTCON_GIE=true -- enable interruptsINTCON_PEIE=true -- enable peripheral interruptsPIE2_CCP2IE=true; --enable ccp2 module interruptT1CON_TMR1ON=on --enable timer1 as timer, no prescalerCCP2CON=5; --set ccp module to interrupt on each rising edgealias dccinput is pin_ccp2 -- input pin for ccp modulepin_ccp2_direction=input--------------------------------------------------------------------------------procedure isr ispragma interruptlcd_cursor_position(0,0)print_byte_dec(lcd,ccpr2h)lcd_cursor_position(1,0)print_byte_dec(lcd,ccpr2l)PIR2_CCP2IF=falseend procedure

author: Andy Watson
author_rank: Super Contributor
timestamp: April 18, 2015, 08:18:14 pm
content: Is that all the code? What does it *do* when it is not in the interrupt ?

author: conducteur
author_rank: Regular Contributor
timestamp: April 18, 2015, 10:13:07 pm
content: idiot I am! Wrote the code for CCP2, but connected the square wave to CCP1 inputNow it's working, but not the "right" values... I expect to approximately 120, but that's not the case here....

author: Andy Watson
author_rank: Super Contributor
timestamp: April 18, 2015, 11:07:39 pm
content: Quote from: conducteur on April 18, 2015, 10:13:07 pmI don't think you can blame the PIC for that one, (although I've often tried)

author: conducteur
author_rank: Regular Contributor
timestamp: April 19, 2015, 09:58:16 pm
content: I'm not an expert in programming, but want to learn it. Today i tried to detect the preamble sequence in a DCC-packet. But there's a problem.... I expect to see "preamble" on the display, but doesn't detect that sequence and so nothing appears on the display. (explanation is in comments between code)http://www.treinbaanrian.be/elektronica/20150419_decoder.htmWho knows what's going wrong?

author: BloodyCactus
author_rank: Frequent Contributor
timestamp: April 20, 2015, 06:15:08 pm
content: have you looked at the arduino dcc library? the opendcc project? there are lots of arduino dcc code around that you can use to check against your own work and see working code vs your code vs what you expect to see it doing etc.