Results after the “hammering” session today.
Testing setup as follows:
One PIC12F683 acting as Tx module. It output bursts of 15 mS 500Khz carrier on CCP1 pin (pin 5) followed by 5mS no carrier. Pin 2 of the PIC is high when carrier ON and low when carrier is OFF.
Second PIC12F683 acting as Rx is connected to the same 5V supply and has its TOCKI pin (pin 5) connected to CCP1 pin of the Tx PIC. Pin 2 of this PIC drives a LED.
After adding some configuration lines and fix some oldCount speling I ran few simple tests to make sure the connections and configs are OK I ran the sample code.
Unfortunately it did not work and the problem was on the Rx side. Monitoring the signals from Tx they were what they should be. Rx will output some very random signals or no signals at all.
So I started messing with the code and try few alterations of the Rx program. I realized that some different handling of TMR0 and oldCount needs to be implemented. I have to confess that some of the results did not make sense to me at the time but by pure luck (I left a code line behind in the first WHILE / WEND loop got some meaningful results).
I do not know enough about the insides of the WHILE / WEND loop but, after violating its condition. I also think I broke few other rules of programming and according to the well known rule if something works there must be at least two mistakes that compensate each other.
The Rx and Tx codes that follow are working and my old analog scope shows perfectly matched signals on the Tx and Rx PICs.
Tx code:
Code:
@ device pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
DEFINE OSC 8 ' We're running at 8 Mhz
OSCCON=%01110111 ' 8MHz internal osc
ANSEL = 0 ' Digital only for all PortA pins
TRISIO=%00100000 ' Make PORTA outputs
out var GPIO.5
CCP1CON = %00001100 ' Normal PWM
PR2 = 3 ' 1MHz output @8MHz system clock
CCPR1L = 2 ' 50% Dutycycle
CCP1CON.5 = 0
CCP1CON.4 =0
T2CON.2 = 1
Main:
high out
T2CON.2=1 ' Osc ON
PAUSE 15
low out
T2CON.2=0 ' Osc OFF
PAUSE 5
Goto Main
Rx code:
Code:
@ device pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
DEFINE OSC 8 ' We're running at 8 Mhz
OSCCON=%01110111 ' 8MHz internal osc
ANSEL = 0 ' Digital only for all pins
TRISIO=%00100100 ' Make PORTA outputs
oldCount VAR byte
LED VAR GPIO.5
oldCount = 0 'Clear count
LED = 0 ' Turn off LED
' Set TMR0 up as a counter, signal fed into T0CKI pin
OPTION_REG.5 = 1
Main:
While TMR0 <> oldCount ' There has been pulses since last time we checked
high led
oldCount = TMR0 ' Store current count
Wend
low led
oldCount =0
TMR0=0
While oldCount=TMR0
low led
wend
GOTO main
Can somebody explain me why this code is working?
Regards,
Nick
Bookmarks