PDA

View Full Version : Someone Posts a Code Example, Then What?



jhorsburgh
- 6th June 2008, 08:48
Hello Everyone,
As you will no doubt gather from the following question, i am very new to PIC's, PIC Programmers and their corresponding code. I have a question about two peices of code i found posted in the forum archives;

Exibit A
<code>
'-----PIC12F629-----
' Sends 2 bytes + their bitwise complements using a variation of the NEC IR protocol
' repeats every 15 seconds
' I've used this (and seen it used in commercial RF products) using 12-48 bits
' with lead-in pulses from 2.5-9.5mS and lead-in spaces from 2.5-4.5mS

@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _CP_OFF & _CPD_OFF

RF VAR byte[4]
Copies VAR byte 'RF copies
c VAR byte 'loop index (RF copies)
b VAR byte 'loop index (RF[b])
i VAR byte 'bit index
wb VAR byte 'work byte

CMCON = 7
Copies = 4
'Put data in RF[0] & RF[2]
SendRF: RF[0]=80:RF[1]=~RF[0]:RF[2]=66:RF[3]=~RF[2]
Low 4
For c=1 To Copies
PulsOut 4, 880 '8.8mS lead-in pulse
PauseUs 4400 '4.4mS space
For b=0 To 3
wb=RF[b]
For i=0 To 7 'LSB first
PulsOut 4, 50 '0.5mS pulse
If wb.0=1 Then
PauseUs 1500 '1.5mS space
Else
PauseUs 500 '0.5mS space
EndIf
wb=wb>>1
Next
Next
PulsOut 4, 50 '0.5mS pulse
Pause 40: '40mS GAP
Next
Sleep 15 '15 SEC DELAY
GoTo SendRF

End
</code>

And Exibit B
<code>
'-----PIC12F629-----
'Receives 32 bits of NEC protocol
'RF with initial lead-in of 8.8mS
'sends codes via RS232 @ 9600bps on GPIO.2

@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _CP_OFF & _CPD_OFF

DEFINE PULSIN_MAX 968 '>968 RETURNS 0
DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 2 'GPIO.2
DEFINE DEBUG_MODE 1 'Inverted
DEFINE DEBUG_BAUD 9600
DEFINE OSCCAL_1K 1

RF VAR byte[4]
space VAR byte
i VAR byte
bits VAR byte
stx VAR word 'start of transmission

CMCON = 7 'comparators off

init: RF[0]=0:RF[1]=0:RF[2]=0:RF[3]=0
bits=0:i=0
PulsIn GPIO.1, 1, stx
If (stx<792) Then init
While GPIO.1=0:Wend 'wait pulse
Repeat
PulsIn GPIO.1, 0, space
If (space<40) Or (space>175) Then init
If (space>75) Then
RF.0(i)=1 'set bit
EndIf
i=i+1
Until (i>31)
If RF[0]+RF[1]<>&HFF Then init
If RF[2]+RF[3]<>&HFF Then init
For i = 0 to 3
Debug (RF[i] REV 8)
Next
GoTo init

End
</code>

How would i wire up the two circuits or better yet does anyone have a wiring diagram for the two circuits? I am pretty sure i have all the components eg. the PIC12F629's, the wireless modules, RS232 plug, capacitors and a power supply.

Any help in this matter would be very much appreciated.
Thanks All,
Jeremy - Aspiring PIC Programmer

Melanie
- 6th June 2008, 09:31
You ask "then what"? Like we all know what you want to do!

So, let's make an assumption that you actually want to build this.

Transmitter...
-----------

1. Get the PIC Datasheet from the Microchip website.
2. Get the PICBasic manual
3. Line by line, look at the code and understand what it's doing...

4. From the Datasheet "Special Features of the CPU" section (the manual and searching this forum) you will discover that the _INTRC_OSC_NOCLKOUT and _MCLRE_OFF settings in the code mean that your PIC will work on it's Internal Oscillator (so no crystal, resonator or other clock circuitry is required) and that it will also handle it's RESET circuitry internally - so no other hadware for that is required externally. So you should just be able to put volts on the PIC and it will work - and the pins for that, along with the voltage that you will need, you can figure from the PICs Datasheet. As for the Power Supply, what are you going to use? 3v Button cell, some other Battery? A wall cube? A nuclear power plant? Some you can connect directly (eg nuclear power plant), some may need some kind of regulator or PSU...

5. From the PICBasic manual you will discover that PULSOUT is explained in detail. You can then discover what pin the signal is falling out of (Pin 4 is not nescessarilly the same pin number on the PIC). Build the circuit and see which pin is pulsing... it could be GPIO.4 (GP4) but it might not be...

6. What is this pin then going to be connected to? If it's an IR Transmitter, then for very short range there are IR emitters that can be connected directly, some which need a series Resistor, and some that need an external Transistor to give a much higher current drive. So which one are you going to use? Make an informed choice. Try Google and study the Datasheet of your chosen device to determine it's suitability for your purpose. You say you have IR modules already - get the Datasheet for them - it'll tell you how to hook them up. How can we possibly even start with a circuit if we don't know what modules you've got! If you told us what you had (which you didn't), all we would do is look it up in the Datasheet - so why not do it yourself - it's good experience!

Receiver
--------

OK... over to you... based on the above 'self-help' instant guide on how you're going to become an IR expert... and based on the clues embedded in the code, what are you going to do first? (clue - look at the code and COMPARE it against the commands listed in the PICBASIC manual... it's all there including the wiring for RS-232).

jhorsburgh
- 10th June 2008, 12:10
Thanks Melanie,
I have had a read over the PIC Basic Manual and now understand most of the code, i have previously dealt with the .asm file format so i am not quite sure how to compile the code to either an asm or hex format. i have tryed microcode studio, melabs and proton but i must be doing something wrong because i can't select the right device let along compile?

I apologise if i am asking to much or if the questions are stupid.
Thanks heaps for your help,
Jeremy

dhouston
- 10th June 2008, 13:17
I don't think I have schematics for each that use the PIC12F629 but my web page does have schematics of the basic circuits using similar chips (although they may use different pins). See...http://davehouston.org/adc.htm
http://davehouston.org/generic-RF-RCVR.htmFor the transdmitter, I used a chip with ADC so I can modify the output to signal a low battery. If you are not interested in that, use the PIC12F629 and omit the connection to pin 6. Because I wanted to maximize battery life, the transmitter is powered by the data signal so there is no transmitter drain when idle.

For the receiver circuit you can use a PIC12F675 or the PIC12F683, as shown, if you want to measure received signal strength using an ADC pin (code will need modification). Otherwise, a PIC12F629 is fine.

NOTE: While the code uses a protocol developed about 40 years ago by NEC for IR remote control (TV, etc.), it's being used here for RF (as X-10 has used it for for nearly 40 years). If you want to use it with IR it's a bit different for both transmitting and receiving.

jhorsburgh
- 10th June 2008, 13:45
Hey Dave,
I plan on using IR in the future but for the moment i would like to get the RF working, the RS232 portion of the reciever is also something i would like to play with so the code you have posted is a perfect start.

Thanks again,
Jeremy

dhouston
- 10th June 2008, 14:53
The primary differences for IR are that you need to modulate the transmitter with the IR carrier frequency (usually 36-40kHz) and IR receivers are active low whereas RF receivers are active high. If you wander about my website you'll find a few schematics that may help. You can find more specific schematics on Bruce Reynols website...http://www.rentron.comMake sure that you are using the latest version of Code Studio. I believe the sample code was written for use with MPASM. If you want to use the meLabs PM assembler, you need to change the fuse config settings - there's a thread here that details the different syntaxes...http://www.picbasic.co.uk/forum/showthread.php?t=543&highlight=config+fuse+preset