So this is what I have so far, <b>it works</b>. When I press a button the corresponding LED lights up and stays alight until another button is pressed.
I'm not so sure why each LED goes out when the next one is lighted, following the code it seems like it should stay lit until eventually after pressing all the buttons all are alight, at which point the device would have to be reset to turn them off.
The PIC with Buttons
Code:
'****************************************************************
'* Name : Output1.pbp *
'* Author : Greensasquatch *
'* Date : 10/11/2008 *
'* Chip : PIC16F628A *
'* Version : 1.0 *
'* Notes : To output a number corresponding to which button *
'* : was pressed via serial communication *
'****************************************************************
'* This Program is part 1 of 2. The other half receives a
'* number and lights a corresponding LED.
'****************************************************************
'*
'* Connections are as follows:
'*
'* N/C -01-|####|-18- Green Power LED
'* N/C -02-|####|-17- SEROUT Line
'* N/C -03-|####|-16- N/C
'* MCLR 10K to V+ -04-|####|-15- N/C
'* Gnd -05-|####|-14- V+
'* Butt1 -06-|####|-13- N/C
'* Butt1 -07-|####|-12- N/C
'* Butt1 -08-|####|-11- N/C
'* Butt1 -09-|####|-10- N/C
'*
'****************************************************************
INCLUDE "MODEDEFS.BAS"
'uses internal oscillator (should use crystal but I ran out)
@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT
GreenLED var PORTB.4
Butt1 var PORTB.0
Butt2 var PORTB.1
Butt3 var PORTB.2
Butt4 var PORTB.3
Pause 2000 'wait to begin
high greenled 'turn on status light
Loop:
IF BUTt1 = 1 THEN SEROUT PORTA.0,T1200,[9,1] 'sends 9 and 1 if butt1 is high
IF BUTt2 = 1 THEN SEROUT PORTA.0,T1200,[9,2] 'sends 9 and 2 if butt2 is high
IF BUTt3 = 1 THEN SEROUT PORTA.0,T1200,[9,3] 'sends 9 and 3 if butt3 is high
IF BUTt4 = 1 THEN SEROUT PORTA.0,T1200,[9,4] 'sends 9 and 4 if butt4 is high
GOTO LOOP
end
The PIC with LEDs
Code:
'****************************************************************
'* Name : Input1.pbp *
'* Author : Greensasquatch *
'* Date : 10/11/2008 *
'* Chip : PIC16F628A *
'* Version : 1.0 *
'* Notes : To read a number via serial communication and *
'* : light a corresponding LED *
'****************************************************************
'* This Program is part 2 of 2. The other half sends a
'* number corresponding to a button state.
'****************************************************************
'*
'* Connections are as follows:
'*
'* LED3 -01-|####|-18- LED2
'* LED4 -02-|####|-17- LED1
'* N/C -03-|####|-16- N/C
'* MCLR 10K to V+ -04-|####|-15- N/C
'* Gnd -05-|####|-14- V+
'* Serial In -06-|####|-13- N/C
'* Green Power LED -07-|####|-12- N/C
'* N/C -08-|####|-11- N/C
'* N/C -09-|####|-10- N/C
'*
'****************************************************************
INCLUDE "MODEDEFS.BAS"
'uses internal oscillator (should use crystal but ran out)
@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT
GreenLED var PORTB.1
LED1 var PORTA.0
LED2 var PORTA.1
LED3 var PORTA.2
LED4 var PORTA.3
NET var byte 'holding variable for input
Pause 2000 'wait to begin
high greenled 'turn on status light
Loop:
SERIN PORTB.0,T1200,[9],NET 'waits for a "9" and writes the next value to NET
IF NET = 1 THEN HIGH led1
IF NET = 2 THEN HIGH led2
IF NET = 3 THEN HIGH led3
IF NET = 4 THEN HIGH led4
GOTO LOOP
end
Now I'm just going to look into how to make it so that the LED only stays lit as long as the button is held down.
Any comments would be great as the finished code will be posted in code examples (with proper credits of course).
In response to Steve's comment:
I left out setting the idle state for now, it works, so I won't change anything until I fully understand why. I had always thought that TTL idle was low and Inverted (and RS232) idle was high. I'm hoping to make this into a wireless setup somewhere down the road, in which case I would want the idle state to be low so the device does not transmit when there is now data. I see I have more research to do.
Bookmarks