#include <reg66x.h>
/////////////////////////
//// PIN DEFINITIONS ////
/////////////////////////
// Port 0 - in - in from CPLD
#define inDigits P0
// Port 1 - all out - out to CPLD
sbit outClock = P1^0;
sbit outReset = P1^1;
// Port 2 - all out - out to display
#define outDisplay P2
// Port 3 - in/out - buttons & speaker & CPLD control
sbit btnStartStop = P3^3; // in
sbit outAcknowledge = P3^7;// out
/////////////////////////////
//// DECLARE SUBROUTINES ////
/////////////////////////////
void setupTimer();
void setupInputs();
void runningLoop();
void timerCallback();
void digitsCallback();
void controlCallback();
void resetCPLD();
///////////////////////////
//// DECLARE VARIABLES ////
///////////////////////////
int modeState = 0;
unsigned char hunths, tenths, seconds, tenSecs;
////////////////////////
//// PROGRAMME CODE ////
////////////////////////
void main() {
setupTimer();
setupInputs();
resetCPLD();
runningLoop();
}
void runningLoop() {
while(1){
outDisplay = tenSecs + 64;
outDisplay = seconds + 128;
outDisplay = tenths + 192;
outDisplay = hunths + 0;
}
}
void resetCPLD(){
outReset = 1;
outReset = 0;
return;
}
void setupTimer() {
TMOD = 0x01; // M0 = 1 (Timer mode 1 - 16 bit mode)
TL0 = 0xFF; // 400Hz = 2304 delay count, 65535-2304 = 63231
TH0 = 0xF6; // TH0 = 0xF6 :: TL0(0xFF) = 0xF6FF = 63231
ET0 = 1; // T0 Interrupt enabled.
EA = 1; // Interrupts enabled.
TR0 = 1; // Begin timer.
P2 = 0x00000000;
return;
}
void setupInputs() {
EA = 1; // Interrupts enabled.
IT0 = 1; // Set on falling edge.
IT1 = 1; // Set on falling edge.
EX0 = 1; // Enable external interrupt 0.
EX1 = 1; // Enable external interrupt 1.
modeState = 0;
outAcknowledge = 0;
return;
}
//////// INTERRUPT CALLBACKS ////////
void timerCallback() interrupt 1 using 2 {
TR0 = 0;
TL0 = 0xFF; // 400Hz = 2304 delay count, 65535-2304 = 63231
TH0 = 0xF6; // TH0 = 0xF6 :: TL0(0xFF) = 0xF6FF = 63231
TF0 = 0;
TR0 = 1; // Begin timer.
outClock =~ outClock; // Invert the clock output pin.
}
void digitsCallback() interrupt 0 {
if(modeState == 0){
seconds = inDigits & 0x0f;
}else if(modeState == 1){
tenSecs = inDigits & 0x0f;
}else if(modeState == 2){
hunths = inDigits & 0x0f;
}else if(modeState == 3){
tenths = inDigits & 0x0f;
}
if(modeState < 3){
modeState++;
}else{
modeState = 0;
}
outAcknowledge = 1;
outAcknowledge = 0;
}
void controlCallback() interrupt 2 {
TR0 =~ TR0;
}
See also:
DigitalSystems.CW2.VHDL DigitalSystems.CW2.VHDLTestBench
Categories:
CategoryUni