Koden till Insamlingsapplikationen

 

 

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

 *                                              *

 *  Processor program                           *

 *                                              *                                                                                                                                                                                   

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

 

char* sramAddr; /* Address to sram */

char* multiAddr;  /* Address to multi purpose unit */

char* radioAddr;  /* Address to RADIO unit */

char* rtcAddr;  /* Address to real time clock */

char* dtmfAddr; /* Address to DTMF receiver */

char* epromAddr; /* Address to EPROM chip */

 

/* Variables */

char *addr; /* current address */

char currOpto1; /* Value OPTO 1 */

char currOpto2; /* Value OPTO 2 */

char prevOpto1;

char prevOpto2;

 

/* Parameters for current call */

char startTime[6];

char endTime[6];

char calledNbr[20];

int calledNbrI;

int state;

 

/* Parameters for radio transmission */

char sendData[100];

char tmpChar;

int sendDataCounter;

 

/* Init functions */

void multiInit(); /* Init serial, timers & interrupts MC68901 */

void radioInit(); /* Init USART for Radio Transmission */

 

/* Misc functions */

void wait(); /* Wait-function */

void waitLong(); /* Function for stabilizing OPTO values */

void readClk();

void sendDataStream(); /* Send call data to PC */

void writeRadio(char);

char readPC();

void writePC(char);

 

/* Interrupt functions */

void exp1(); /* DTMF */

void exp2(); /* PHONE */

 

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

 *  Main program

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

 

main() {

       calledNbrI = 0;

       currOpto1 = 0x00; /* Value OPTO 1 */

       currOpto2 = 0x00; /* Value OPTO 2 */

       prevOpto1 = 0x10;

       prevOpto2 = 0x08;

       state = 0;

       sendDataCounter = 0;

 

       epromAddr = (char*) 0x0000; /*0x0000-0x1FFF*/     

       sramAddr = (char*) 0x2000; /*0x2000-0x3FFF*/

       dtmfAddr = (char*) 0x4000; /*0x4000-0x5FFF*/

       rtcAddr = (char*) 0x8000; /*0x8000-0x9FFF*/

       multiAddr = (char*) 0xC000; /*0xC000-0xDFFF*/

       radioAddr = (char*) 0x6000; /*0x6000-0x7FFF*/

      

       /* Do initialization */

       multiInit();

       radioInit();

 

       /* Enable tramsmitter in Transmitter Status Register (TSR). Port 16*/

       addr = (char*) 0xC016;

       *addr = 0x01; /*0000 0001*/

 

       writePC('-'); /* init communication? */

       writePC('-'); /* init communication? */

 

       /* Set end of transmission and disable transmitter */

       addr = (char*) 0xC016;

       *addr = 0x10; /*0001 0000*/

 

       _avben(); /* enable interrupt in proc */

 

       inf: goto inf;

      

}

 

/* Initiate the Multifunction MC68901 component (RADIO) */

void radioInit() {

 

       /* Disable external interrupts */

      

       /* Set Interrupt Enable Register A (IERA). Port 3 */

       addr = (char*) 0x6003;

       *addr = 0x00; /*0000 0000*/

 

       /* Set Interrupt Enable Register B (IERB). Port 4 */

       addr = (char*) 0x6004;

       *addr = 0x00;  /*0000 0000*/

 

       /* Set all I/O to outputs */

       addr = (char*) 0x6002;

       *addr = 0xFF; /*1111 1111*/

 

 

       /* Setup timer for baud rate 9600 */

 

       /* Set up timer output D to % 16 of the input frequency (TCDCR). Port E*/

       addr = (char*) 0x600E;

       *addr = 0x03; /*0000 0011*/

 

       /* Set up timer clock mode */

       addr = (char*) 0x6012;

       *addr = 0x0C; /*0000 1100*/

 

 

       /* Setup USART registers */

 

       /* Set USART Control Register (UCR). Port 14*/

       addr = (char*) 0x6014;

       *addr = 0x08; /*0000 1000*/

      

       /* Disable receiver in Receiver Status Register (RSR). Port 15*/

       addr = (char*) 0x6015;

       *addr = 0x00; /*0000 0000*/

 

       /* Disable tramsmitter in Transmitter Status Register (TSR). Port 16*/

       addr = (char*) 0x6016;

       *addr = 0x01; /*0000 0000*/

 

}

 

/* Initiate the Multifunction MC68901 component (PC) */

void multiInit() {

      

       /* Disable external interrupts */

      

       /* Set Interrupt Enable Register A (IERA). Port 3 */

       addr = (char*) 0xC003;

       *addr = 0x00; /*0000 0000*/

 

       /* Set Interrupt Enable Register B (IERB). Port 4 */

       addr = (char*) 0xC004;

       *addr = 0x00;  /*0000 0000*/

 

      

       /* Setup timer */

 

       /* Set up timer output D to % 16 of the input frequency (TCDCR). Port E*/

       addr = (char*) 0xC00E;

       *addr = 0x03; /*0000 0011*/

 

       /* Set up timer clock mode */

       addr = (char*) 0xC012;

       *addr = 0x06; /*0000 0110*/

 

 

       /* Setup USART registers */

 

       /* Set USART Control Register (UCR). Port 14*/

       addr = (char*) 0xC014;

       *addr = 0x08; /*0000 1000*/

      

       /* Disable receiver in Receiver Status Register (RSR). Port 15*/

       addr = (char*) 0xC015;

       *addr = 0x00; /*0000 0000*/

 

       /* Disable tramsmitter in Transmitter Status Register (TSR). Port 16*/

       addr = (char*) 0xC016;

       *addr = 0x00; /*0000 0000*/

 

 

       /* Setup INTERRUPT registers */

 

       /* Set Active Edge Register (AER). Port 1 */

       addr = (char*) 0xC001;

       *addr = 0x01; /*0000 0001*/

 

       /* Set Data Direction Register (DDR). Port 2*/

       addr = (char*) 0xC002;

       *addr = 0x00; /*0000 0000*/

 

       /* Clear Interrupt Pending Register A (IPRA). Port 5 */

       addr = (char*) 0xC005;

       *addr = 0x00; /*0000 0000*/

 

       /* Clear Interrupt Pending Register B (IPRB). Port 6 */

       addr = (char*) 0xC006;

       *addr = 0x00; /*0000 0000*/

 

       /* Clear the Interrupt In Service Register A (ISRA). Port 7*/

       addr = (char*) 0xC007;

       *addr = 0x00; /*0000 0000*/

 

       /* Clear the Interrupt In Service Register B (ISRB). Port 8*/

       addr = (char*) 0xC008;

       *addr = 0x00; /*0000 0000*/

 

       /* Set Interrupt Mask Register A (IMRA). Port 9*/

       addr = (char*) 0xC009;

       *addr = 0x0A; /*0000 1010*/

 

       /* Set Interrupt Mask Register B (IMRB). Port A*/

       addr = (char*) 0xC00A;

       *addr = 0x03; /*0000 0011*/

 

       /* Set the Vector Register (VR). Port B*/

       addr = (char*) 0xC00B;

       *addr = 0x40; /*0100 0000*/

 

 

       /* Enable interrupts */

 

       /* Set Interrupt Enable Register A (IERA). Port 3 */

       addr = (char*) 0xC003;

       *addr = 0x00; /*0000 0000*/

 

       /* Set Interrupt Enable Register B (IERB). Port 4 */

       addr = (char*) 0xC004;

       *addr = 0x03;  /*0000 0011*/

}

 

char readPC() {

       char c;

       int counter = 0;

 

       addr = (char*) 0xC015; /* choose RSR*/

       while(((*addr & 0x80) != 0x80)& (counter < 0xffffffff)){ /* while buffer is empty wait */

              counter = counter + 1;

       }

 

       addr = (char*) 0xC017; /* Choose UDR */

 

       if(counter == 0xff) {

              c = '='; /* Read one byte of data from UDR */

       }else{

              c = (char) *addr; /* Read one byte of data from UDR */

       }

 

       return c;

}

 

void writePC(char data) {

       addr = (char*) 0xC016; /* Choose TSR in multifunction unit */

       while((*addr & 0x80) != 0x80); /* while buffer is full wait */

 

       addr = (char*) 0xC017; /* Choose UDR */

       *addr = data; /* Write one byte of data in to UDR for transmission to PC */

}

 

void writeRadio(char rdata) {

       addr = (char*) 0x6016; /* Choose TSR in multifunction unit */

       while((*addr & 0x80) != 0x80); /* while buffer is full wait */

 

       addr = (char*) 0x6017; /* Choose UDR */

       *addr = rdata; /* Write one byte of data in to UDR for transmission to PC */

}

 

void readClk() {  /* Read time */

       int i;

       for(i = 0; i < 6; ++i) {

              if(state == 0){

                     startTime[i] = '3';

              }else if (state == 1) {

                     endTime[i] = '4';

              }

       }

}

 

void sendDataStream() {  /* Send data to PC */ 

      

       int boole = 0;

       int i;

 

       /* Enable tramsmitter in Transmitter Status Register (TSR). Port 16*/

       addr = (char*) 0xC016;

       *addr = 0x01; /*0000 0001*/

 

       writePC('-'); /* DUMMY */

       writePC('='); /* Write start character */

       writePC('#'); /* PC command receive number */

       writePC(';'); /* Write separator character */

      

       for(i = 0; i < 6; ++i) {

              writePC((char) startTime[i]); /* Write start time */       

       }

       writePC(';'); /* Write separator character */

       for(i = 0; i < 6; ++i) {

              writePC((char)endTime[i]); /* Write end time */

       }

       writePC(';');/* Write separator character */

       for(i = 0; i < calledNbrI; ++i) {

              writePC((char)calledNbr[i]);/* Write called number */

       }

       writePC(';'); /* Write separator character */

       writePC('='); /* Write end character */

       writePC('-'); /* DUMMY */

 

       /* Set end of transmission and disable transmitter */

       addr = (char*) 0xC016;

       *addr = 0x10; /*0001 0000*/

      

       waitLong();

      

       /* Send data to radio */

 

       /* Enable tramsmitter in Transmitter Status Register (TSR). Port 16*/

       addr = (char*) 0xC016;

       *addr = 0x01; /*0000 0001*/

 

       writePC('-'); /* DUMMY */

       writePC('='); /* Write start character */

       writePC('&'); /* PC command receive request */

       writePC(';'); /* Write separator character */ 

 

       writePC('>');

 

       writePC('='); /* Write end character */

       writePC('-'); /* DUMMY */

 

       /* Set end of transmission and disable transmitter */

       addr = (char*) 0xC016;

       *addr = 0x10; /*0001 0000*/

 

       /* Enable receiver get data for receiver */

       addr = (char*) 0xC015;

       *addr = 0x01; /*0000 0001*/

 

       while(boole == 0) {

              tmpChar = readPC();

              if(tmpChar == '=' && sendDataCounter > 2) {

                     boole = 1;

              } else {

                     sendData[sendDataCounter] = tmpChar;

                     sendDataCounter = sendDataCounter + 1;

              }

       }

 

        /* Disable receiver PC */

       addr = (char*) 0xC015;

       *addr = 0x00; /*0000 0000*/

 

       /* Send data to radio */

 

       writeRadio('=');

 

       for(i = 0; i<sendDataCounter; i++) {

              writeRadio(sendData[i]);

       }

       sendDataCounter = 0;

             

       writeRadio('=');

 

}

 

 

void exp1()               /* avbrottsprogram DTMF */

{

       if(state != 1){

              char c = (char) (*dtmfAddr & 0x0F);

              if(c != 0x0A)

                     calledNbr[calledNbrI] = c + 0x30;

              else

                     calledNbr[calledNbrI] = '0';

              if(calledNbrI < 20) {

                     calledNbrI = (calledNbrI+1);

              }

       }else{

              calledNbr[calledNbrI] = 'E';

              calledNbrI = (calledNbrI+1);

       }

       return;

}

 

void exp2()               /* avbrottsprogram PHONE */

{

       waitLong();  /* Wait for stable OPTO values */

       addr = (char*) 0xC000;

       currOpto1 = (*addr) & 0x10; /* Mask: 0001 0000 */

       currOpto2 = (*addr) & 0x08; /* Mask: 0000 1000 */

 

       if((currOpto1 == prevOpto1) & (currOpto2 == prevOpto2)) {

              if((currOpto1 == 0x10) & (currOpto2 == 0x08)) {

                     calledNbrI = 0;

                     state = 0;

              }

              return;

       }

 

       /* Nedkoppling av samtal (Motpart lägger på lur/Vi lägger på lur) */

       if((state == 1) &

              (currOpto1 != prevOpto1) & /* Båda opto ändrade */

              (currOpto2 != prevOpto2))

       {

              readClk();  /* Read end time */

              sendDataStream();

              /* Data sent to PC clear calledNbrI */

              calledNbrI = 0;

              state = 2;

       }

       else if((state == 1) &

              (currOpto1 == 0x10) &

              (currOpto2 == 0x08))

       {

              readClk();  /* Read end time */

              sendDataStream();

              /* Data sent to PC clear calledNbrI */

              calledNbrI = 0;

              state = 0;

       }

 

       /* Luren ligger pa/läggs på */

       else if((currOpto1 == 0x10) &

              (currOpto2 == 0x08))

       {

              calledNbrI = 0;

              state = 0;

             

       }

       /* Uppkoppling av samtal */

       else if((state == 0) &

              ((currOpto1 != prevOpto1) & (currOpto2 == prevOpto2)) |

              ((currOpto1 == prevOpto1) & (currOpto2 != prevOpto2))) /* En opto ändrad och state==0*/

       {

              readClk(); /* Read start time */

              state = 1;

       }

       else {

              state = 0;

              calledNbrI = 0;

       }

 

 

       prevOpto1 = currOpto1;

       prevOpto2 = currOpto2;

      

       /* Anm: Anrop till readClk() ska göras INNAN onGoingCall sätts till true/false */

 

       return;

}

 

 

/* Delay function */

 

void wait() {

       int i;

       for (i = 0; i < 0x55; i++) {

       }

}

 

void waitLong() {

       int i;

       for (i = 0; i < 0xFFFF; i++) {

       }

}