#include "p16f84.h"
#include "serial.h"
#include "network.h"
#pragma CLOCK_FREQ 12000000
// Configuration Bits Oscillator=XP, all fuse OFF
asm __config H'3FF9'
// Function definition
void Init ();
char GetKey ();
// Address of this slave, change it for each slave
#define ADDRESS 1
// this is a delay that every slave have to wait multiplied his address, so
// the slave 1 wait 5 ms, the slave 2 wait 10 ms and so on
// in this case the pc send a broadcast message, and every slave reply after a specified
// time, so the Master (PC) doesnt need to poll every slave, just send a message on the
// bus and who is on line just send a "alive" message
// this is usually called time slot network?? i dont remember if is the right name...
#define MAIN_TX_DELAY 5
main()
{
char Led_Verde=1; // Leds on my board GREEN
char Led_Giallo=0; // Leds on my board YELLOW
char Led_Rosso=0; // Leds on my board RED
int i;
char Key;
Init ();
while (1)
{
// Turn Led on on Port A
if (Led_Verde) porta|=4; else porta&=251;
if (Led_Giallo) porta|=8; else porta&=247;
if (Led_Rosso) porta&=239; else porta|=16;
// Check if there is a packet sent from Master
if (Net_Receive(0)==NET_OK)
{
// Time Slot...every slave wait a specified time to permit
// the precedent slave to send the packet
for (i=0;i<ADDRESS;i++) delay_ms (MAIN_TX_DELAY);
// Turn on/Off the RED depending the command sent from master
if (NetBuf[1]==1) Led_Rosso=1;
if (NetBuf[1]==2) Led_Rosso=0;
// Now fill the buffer that the slave send to master
NetBuf[0]=ADDRESS; // The slave address
NetBuf[1]=GetKey(); // Get the status of the keybaord (a simply 12 keys)
// Send the information to the master
Net_Send (NetBuf[0],NetBuf[1]);
// Turn on the Yellow led if a key is pressed
if (NetBuf[1]) Led_Giallo=1; else Led_Giallo=0;
}
}
}
/*
**********************
System initialization
**********************
*/
void Init ()
{
option_reg = 6; // set prescaler to 1:128
trisa = 1; // Set PA0 as input
porta = 0; // Clear porta
trisb = 112; // set PB0..PB3/PB7 as output PB4,PB5,PB6 as input
portb = 15;
MS_TIMER=0;
enable_interrupt( GIE ); // Enable interrupt
enable_interrupt( T0IE ); //enable TMR0 overflow bit
Net_Init (); // Packet initialization
}
// Keyboard mapping
const char Keys[]={'A','0','C','7','8','9','4','5','6','1','2','3'};
/*
Getting the state of the keyboard
this is done scanning each row of the keyboard
*/
char GetKey ()
{
char Row=14;
char Key=0;
char App;
int i;
for (i=0;i<4;i++)
{
portb=Row; // Se the line low and check what input pin is HIGH
App=(portb & 0x70)>>4;
switch (App)
{
case 6:
Key=Keys[i*3];
break;
case 5:
Key=Keys[i*3+1];
break;
case 3:
Key=Keys[i*3+2];
break;
}
Row=Row << 1; // check the next row
Row=Row | 1;
Row=Row & 0x0F;
}
return Key;
}
Copyright © 2002-2006 SourceBoost Technologies