Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

latching switches

Status
Not open for further replies.

gazza1966

New Member
Hi all.

Firstly, I'm a programming virgin and finding this microcontroller programming testing to say the least!
I've managed to make several LED flashing programs using Matrix multimedia's Flowcode software which has made it LOTS easier.
I am now trying to utilise switches in another learning process for the PIC and what I thought would be an easy project has me at a dead end from the start!
I would appreciate any help you programmers could give me.

The project:
I have 4 push to make switches connected to the inputs on portA.
When switch number one is pressed I want an output on portB to illuminate an LED and stay illuminated.
When any of the other switches are then pressed I would like the LED that was switched on by switch one to go off and the next LED that corresponds with that switch to illuminate.
In short switches must latch when pressed and then un-latch when another switch is pressed.

Any help on starting this project would be greatly received.

regards

Gaz
 
Last edited:
hi,
Which PIC and what language do you work with other than Matrix multimedia's Flowcode.?:)
 
Hi Eric

Ive modified existing asm. and C but cant really say that I understand them thats why I went down the Flowcode route.
The PIC I will be using will be the 16F628A.

cheers

Gaz
 
Hi again

This what the Flowcode compiles to in C and asm using one of the examples provided with the software.

//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F628A
#define MX_EE
#define MX_EE_TYPE1
#define MX_EE_SIZE 128
#define MX_UART
#define MX_UART_B
#define MX_UART_TX 2
#define MX_UART_RX 1
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_TRIS1 trisb
#define MX_PWM_1 3

//Functions
#include <system.h>
#pragma CLOCK_FREQ 19660800

//Configuration data

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V3\FCD\internals.h"

//Macro function declarations
void FCM_INTERRUPT_TMR0();
void FCM_INTERRUPT_RB0INT();
void FCM_INTERRUPT_PORTB();


//Variable declarations
char FCV_SWITCHES;
short FCV_SWITCH;
char FCV_HOLD;



//Supplementary defines


//Macro implementations

void FCM_INTERRUPT_TMR0()
{

}

void FCM_INTERRUPT_RB0INT()
{

}

void FCM_INTERRUPT_PORTB()
{

}

//Supplementary implementations


void main()
{
//Input from switches

//Initialisation
cmcon = 0x07;


//Interrupt initialisation code
option_reg = 0xC0;


//Infinite Loop
//Loop: While 1
while (1)
{
//Get input from switches on Port A
//Input: PORT A -> SWITCHES
trisa = trisa | 0xff;
FCV_SWITCHES = porta;


//Send switch data to the LEDs on Port B
//Output: SWITCHES -> PORT B
trisb = 0x00;
portb = FCV_SWITCHES;


}


mainendloop: goto mainendloop;
}

void interrupt(void)
{
}


And in asm:


include "P16F628A.inc"
; Heap block 0, size:95 (0x000000A0 - 0x000000FE)
__HEAP_BLOCK0_BANK EQU 0x00000001
__HEAP_BLOCK0_START_OFFSET EQU 0x00000020
__HEAP_BLOCK0_END_OFFSET EQU 0x0000007E
; Heap block 1, size:76 (0x00000024 - 0x0000006F)
__HEAP_BLOCK1_BANK EQU 0x00000000
__HEAP_BLOCK1_START_OFFSET EQU 0x00000024
__HEAP_BLOCK1_END_OFFSET EQU 0x0000006F
; Heap block 2, size:48 (0x00000120 - 0x0000014F)
__HEAP_BLOCK2_BANK EQU 0x00000002
__HEAP_BLOCK2_START_OFFSET EQU 0x00000020
__HEAP_BLOCK2_END_OFFSET EQU 0x0000004F
; Heap block 3, size:0 (0x00000000 - 0x00000000)
__HEAP_BLOCK3_BANK EQU 0x00000000
__HEAP_BLOCK3_START_OFFSET EQU 0x00000000
__HEAP_BLOCK3_END_OFFSET EQU 0x00000000
gbl_status EQU 0x00000003 ; bytes:1
gbl_indf EQU 0x00000000 ; bytes:1
gbl_tmr0 EQU 0x00000001 ; bytes:1
gbl_pcl EQU 0x00000002 ; bytes:1
gbl_fsr EQU 0x00000004 ; bytes:1
gbl_porta EQU 0x00000005 ; bytes:1
gbl_portb EQU 0x00000006 ; bytes:1
gbl_pclath EQU 0x0000000A ; bytes:1
gbl_intcon EQU 0x0000000B ; bytes:1
gbl_pir1 EQU 0x0000000C ; bytes:1
gbl_tmr1l EQU 0x0000000E ; bytes:1
gbl_tmr1h EQU 0x0000000F ; bytes:1
gbl_t1con EQU 0x00000010 ; bytes:1
gbl_tmr2 EQU 0x00000011 ; bytes:1
gbl_t2con EQU 0x00000012 ; bytes:1
gbl_ccpr1l EQU 0x00000015 ; bytes:1
gbl_ccpr1h EQU 0x00000016 ; bytes:1
gbl_ccp1con EQU 0x00000017 ; bytes:1
gbl_rcsta EQU 0x00000018 ; bytes:1
gbl_txreg EQU 0x00000019 ; bytes:1
gbl_rcreg EQU 0x0000001A ; bytes:1
gbl_cmcon EQU 0x0000001F ; bytes:1
gbl_option_reg EQU 0x00000081 ; bytes:1
gbl_trisa EQU 0x00000085 ; bytes:1
gbl_trisb EQU 0x00000086 ; bytes:1
gbl_pie1 EQU 0x0000008C ; bytes:1
gbl_pcon EQU 0x0000008E ; bytes:1
gbl_pr2 EQU 0x00000092 ; bytes:1
gbl_txsta EQU 0x00000098 ; bytes:1
gbl_spbrg EQU 0x00000099 ; bytes:1
gbl_eedata EQU 0x0000009A ; bytes:1
gbl_eeadr EQU 0x0000009B ; bytes:1
gbl_eecon1 EQU 0x0000009C ; bytes:1
gbl_eecon2 EQU 0x0000009D ; bytes:1
gbl_vrcon EQU 0x0000009F ; bytes:1
gbl_FCV_SWITCHES EQU 0x00000023 ; bytes:1
Int1Context EQU 0x0000007F ; bytes:1
Int1BContext EQU 0x00000020 ; bytes:3
ORG 0x00000000
GOTO _startup
ORG 0x00000004
MOVWF Int1Context
SWAPF STATUS, W
BCF STATUS, RP0
BCF STATUS, RP1
MOVWF Int1BContext
SWAPF PCLATH, W
MOVWF Int1BContext+D'1'
SWAPF FSR, W
MOVWF Int1BContext+D'2'
BCF PCLATH,3
BCF PCLATH,4
GOTO interrupt
ORG 0x00000010
main
; { main ; function begin
MOVLW 0x07
BCF STATUS, RP0
BCF STATUS, RP1
MOVWF gbl_cmcon
MOVLW 0xC0
BSF STATUS, RP0
MOVWF gbl_option_reg
label268439634
MOVLW 0xFF
BSF STATUS, RP0
IORWF gbl_trisa, W
MOVWF gbl_trisa
BCF STATUS, RP0
MOVF gbl_porta, W
MOVWF gbl_FCV_SWITCHES
BSF STATUS, RP0
CLRF gbl_trisb
BCF STATUS, RP0
MOVF gbl_FCV_SWITCHES, W
MOVWF gbl_portb
GOTO label268439634
; } main function end

ORG 0x00000024
_startup
BCF PCLATH,3
BCF PCLATH,4
GOTO main
ORG 0x00000027
interrupt
; { interrupt ; function begin
BCF STATUS, RP0
BCF STATUS, RP1
SWAPF Int1BContext+D'2', W
MOVWF FSR
SWAPF Int1BContext+D'1', W
MOVWF PCLATH
SWAPF Int1BContext, W
MOVWF STATUS
SWAPF Int1Context, F
SWAPF Int1Context, W
RETFIE
; } interrupt function end

END

The example turns the LED's on using push to make switches but does not latch.


I hope this helps the board understand what I'm after.

cheers

Gaz
 
Last edited:
Hi Eric

Ive modified existing asm. and C but cant really say that I understand them thats why I went down the Flowcode route.
The PIC I will be using will be the 16F628A.

cheers

Gaz

hi,
Im not familiar with Matrix, would a simple asm source be OK.
Ive just written a very simple program for 4 switches and 4 leds, in Oshonsoft basic/asm.?
 
hi gaz.

Look thru this asm, its for the 16F628A
 

Attachments

  • ledsw1.asm
    1.8 KB · Views: 120
Hi Eric

Sure, that would be good to have a look at your asm listing.

I get the impression that the Flowcode may not be the answer to my easy learning and i will have to learn to code :eek:


cheers

Gaz
 
Hi Eric

Thats just what I'm after :)

Pass that man a cigar! ;)

I'm going to go through the code now to try and make sense of it all!

thank you

regards

Gaz
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top