' **************************************************************** ' * File: gmbiobe.bas - Ver. 1.1 * ' * Compiler: PIC Basic PRO * ' * IDE: MicroCode Studio Plus * ' * Compiler Version: 2.45 * ' * Boards: GMM 4620 + GMB HR168 * ' * GRIFO(R) via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * by Graziano Gaiba del 31.01.05 * ' **************************************************************** ' ' This demo program allows to se immediatly both the relay outputs ' available respectively on connnectors CN3 and CN4 and the ' optocoupled inputs available respectively on connnectors CN1 and ' CN2. ' ' It is possible to see the status of the sixteen NPN/PNP inputs ' on the console or to set the status of the eight output relays. ' In addition the Mini Module peripherals can perform developed ' features like: hardware counted inputs, etc. ' ' Rel 1.1 - by Graziano Gaiba ' Demo program of buffered I/O peripherals of GMB HR168 driven by ' Mini Module GMM 4620 ' ' ' ******************** Compiler definitions ********************** ' DEFINE OSC 10 ' Oscillator frequency 9,8304 MHz DEFINE HSER_RCSTA 90h ' Enable EUSART serial receiver DEFINE HSER_TXSTA 20h ' Enable EUSART serial transmitter DEFINE HSER_BAUD 19200 ' Baud Rate ' ' ******************* Constants declaration ********************** ' RTCSLA con $A0 ' ' ******************** Variables declaration ********************* ' ' Generic variables i var byte scelta var byte di var byte do var byte ' ' Used by I/O management procedures port_val var byte ' Value read or to be written to the I/O ' ' ' Used by interrupts cnt_int0 var byte cnt_int1 var byte ' ' ' Used by Timer 0 and 1 val_tmr0 var word val_tmr1 var word cnt_tmr1_int var byte ' ' '************************** Main Program ******************************* ' main: gosub Init_cpu ' Turn OFF relays port_val = 0 gosub set_relays for_ever: gosub clrscr hserout ["Buffered I/O demo Rel 1.1 for GMM 4620 rel 120304 and GMB HR168 rel 110104", 13, 10, 13, 10] hserout ["1) Digital inputs", 13, 10] hserout ["2) Relay outputs", 13, 10] hserout [13, 10, "Scelta: "] input_scelta: hserin [dec1 scelta] if scelta < 1 and scelta > 2 then input_scelta hserout [dec1 scelta, 13, 10] if scelta = 1 then gosub digital_inputs if scelta = 2 then gosub relay_outputs goto for_ever end ' ' **************** Procedures definition ****************** ' ' ' CPU and signals directions initialization Init_cpu: ADCON1=$0f ' Imposta come I/O digitale i pin AN0..12 CMCON=$07 ' Imposta come I/O digitale RA0..4 ' Optocoupled Inputs of CN1 are: ' IN1-1 <-> RA0 ' IN2-1 <-> RA1 ' IN3-1 <-> RB0 ' IN4-1 <-> RB1 ' IN5-1 <-> RA4 ' IN6-1 <-> RC0 ' IN7-1 <-> RC1 ' IN8-1 <-> RC5 trisa.0 = 1 trisa.1 = 1 trisa.4 = 1 trisb.0 = 1 trisb.1 = 1 trisc.0 = 1 trisc.1 = 1 trisc.5 = 1 ' Optocoupled Inputs of CN2 are: ' IN1-2 <-> RD0 ' IN2-2 <-> RD1 ' IN3-2 <-> RD2 ' IN4-2 <-> RD3 ' IN5-2 <-> RD4 ' IN6-2 <-> RD5 ' IN7-2 <-> RD6 ' IN8-2 <-> RD7 trisd = $ff ' Relay outputs of CN3 are: ' OUT A1 <-> RB4 ' OUT A2 <-> RB5 ' OUT B1 <-> RB6 ' OUT B2 <-> RB7 ' OUT C1 <-> RB3 ' OUT C2 <-> RB2 trisb = trisb & $03 ' Relay outputs of CN4 are: ' OUT D1 <-> RA3 ' OUT D2 <-> RC2 (J10 in position 3-4) trisa.3 = 0 trisc.2 = 0 return ' ' ' Clear screen, sending 25 times CR + LF. clrscr: for i= 0 to 24 hserout [13, 10] next i return ' ' ' Asks for a key press wait_key: hserout ["Press a key..."] wait_key_loop: hserin 1, wait_key_loop, [i] return ' ' ' Procedure to set the status of the relays on connectors CN3 and CN4. ' According to the bits of port_val, each relay is turned ON ' (contact closed) or OFF (contact open). ' Bits of port_val have this meanging: ' -- CN3 ' port_val.0 drives relay OUT A1 ' port_val.1 drives relay OUT A2 ' port_val.2 drives relay OUT B1 ' port_val.3 drives relay OUT B2 ' port_val.4 drives relay OUT C1 ' port_val.5 drives relay OUT C2 ' -- CN4 ' port_val.6 drives relay OUT D1 ' port_val.7 drives relay OUT D2 ' ' Each bit has the following meaning: ' bit Meaning ' 0 Relay turned OFF (contact open) ' 1 Relay turned ON (contact closed) set_relays: ' Relays are driven in complemented logic, so port_val must be ' complemented too port_val = port_val ^ $ff portb.4 = port_val.0 portb.5 = port_val.1 portb.6 = port_val.2 portb.7 = port_val.3 portb.3 = port_val.4 portb.2 = port_val.5 porta.3 = port_val.6 portc.2 = port_val.7 return ' ' ' Procedure to read the status of optocoupled inputs on connector CN1. ' Bits of port_val have this meanging: ' port_val.0 is status of IN1-1 ' port_val.1 is status of IN2-1 ' port_val.2 is status of IN3-1 ' port_val.3 is status of IN4-1 ' port_val.4 is status of IN5-1 ' port_val.5 is status of IN6-1 ' port_val.6 is status of IN7-1 ' port_val.7 is status of IN8-1 ' ' According to the bits of port_val, each input is closed or open. ' Each bit has the following meaning: ' bit Meaning ' 0 Contact open ' 1 Contact closed get_opto_1: port_val.0 = porta.0 port_val.1 = porta.1 port_val.2 = portb.0 port_val.3 = portb.1 port_val.4 = porta.4 port_val.5 = portc.0 port_val.6 = portc.1 port_val.7 = portc.5 ' Optocoupled inputs are driven in complemented logic, ' so port_val must be complemented too port_val = port_val ^ $ff return ' ' ' Procedure to read the status of optocoupled inputs on connector CN2. ' Bits of port_val have this meanging: ' port_val.0 is status of IN1-2 ' port_val.1 is status of IN2-2 ' port_val.2 is status of IN3-2 ' port_val.3 is status of IN4-2 ' port_val.4 is status of IN5-2 ' port_val.5 is status of IN6-2 ' port_val.6 is status of IN7-2 ' port_val.7 is status of IN8-2 ' ' According to the bits of port_val, each input is closed or open. ' Each bit has the following meaning: ' bit Meaning ' 0 Contact open ' 1 Contact closed get_opto_2: port_val = portd ' Optocoupled inputs are driven in complemented logic, ' so port_val must be complemented too port_val = port_val ^ $ff return ' ' ' Demo of optocoupled digital inputs digital_inputs: gosub clrscr hserout ["Demo of optocoupled inputs section", 13, 10, 13, 10] hserout ["Status of inputs of CN1 and CN2 is continuously visualized", 13, 10] hserout ["Press a key to exit.", 13, 10, 13, 10] hserout ["CN1 CN2", 13, 10] digital_inputs_loop: gosub get_opto_1 hserout [" ", hex2 port_val] gosub get_opto_2 hserout [" ", hex2 port_val, 13] pause 200 hserin 1, digital_inputs_loop, [i] hserout [13, 10, 13, 10] ' ' hserout ["IN3 and IN4 of CN1 are connected respectively to interrupts INT0 and INT1.", 13, 10] hserout ["Each falling edge on an input triggers respective interrupt, which increments", 13, 10] hserout ["a counter. The counters are continuously visualized.", 13, 10] hserout ["Press a key to exit.", 13, 10, 13, 10] hserout ["Count of: INT0 INT1", 13, 10] on interrupt goto Interrupts_manager ' Disable priority interrupts rcon.7 = 0 ' Trigger INT0 and INT1 on falling edges intcon2.6 = 0 intcon2.5 = 0 ' Enable INT0 and INT1 intcon.4 = 1 intcon3.3 = 1 ' Peripheral interrupts enable and global interrupts enable intcon.6 = 1 intcon.7 = 1 ' Init interrupts counters cnt_int0 = 0 cnt_int1 = 0 enable interrupts_loop: hserout [" ", hex2 cnt_int0, " ", hex2 cnt_int1, 13] pause 50 hserin 1, interrupts_loop, [i] disable ' Global interrupts disable and peripheral interrupts disable intcon.7 = 0 intcon.6 = 0 ' Disable INT0 and INT1 intcon3.3 = 0 intcon.4 = 0 hserout [13, 10, 13, 10] ' ' hserout ["IN5 and IN6 of CN1 are connected respectively to the external clock of", 13, 10] hserout ["Timer 0 and Timer 1.", 13, 10] hserout ["Each falling edge for Timer 0 and rising edge for Timer 1 triggers timer,", 13, 10] hserout ["which increments its hardware counter.", 13, 10] hserout ["Also, overflow of Timer 1 generate a counted interrupt.", 13, 10] hserout ["The counters are continuously visualized.", 13, 10] hserout ["Press a key to exit.", 13, 10, 13, 10] hserout ["Value of: Timer 0 Timer 1 Count of: Timer 1 overflow interrupt", 13, 10] ' Init counters tmr0h = 0 tmr0l = 0 tmr1h = $ff tmr1l = $f0 ' Init Timer 1 interrupt counter cnt_tmr1_int = 0 on interrupt goto Timer_int_manager ' Configure Timer 0 for 16 bit, trigger falling edges of T0CKI, no prescaler t0con = $b8 ' Configure Timer 1 for trigger falling edges of T13CKI, no prescaler t1con = $83 ' Disable priority interrupts rcon.7 = 0 ' Enable Timer 1 overflow interrupt pie1.0 = 1 ' Peripheral interrupts enable and global interrupts enable intcon.6 = 1 intcon.7 = 1 enable timer_loop: val_tmr0.lowbyte = tmr0l val_tmr0.highbyte = tmr0h val_tmr1.lowbyte = tmr1l val_tmr1.highbyte = tmr1h hserout [" ", hex4 val_tmr0, " ", hex4 val_tmr1] hserout [" ", hex2 cnt_tmr1_int, 13] pause 50 hserin 1, timer_loop, [i] disable ' Global interrupts disable and peripheral interrupts disable intcon.7 = 0 intcon.6 = 0 ' Disable Timer 1 overflow interrupt pie1.0 = 0 return ' ' ' Interrupts manager Interrupts_manager: if intcon.1 = 1 then intcon.1 = 0 cnt_int0 = cnt_int0 + 1 endif if intcon3.0 = 1 then intcon3.0 = 0 cnt_int1 = cnt_int1 + 1 endif resume ' ' ' Timer interrupt manager Timer_int_manager: if pir1.0 = 1 then pir1.0 = 0 cnt_tmr1_int = cnt_tmr1_int + 1 endif resume ' ' ' Demo of relay output relay_outputs: gosub clrscr hserout ["Demo of relay outputs section", 13, 10, 13, 10] repeat hserout ["Insert two UPPERCASE hex digits to set relay status (00 to exit): "] hserin [hex2 do] hserout [hex2 do, 13, 10] port_val = do gosub set_relays until do == 0 return