Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** D A T A B U F F E R S A N D F L A G S **********************/
- unsigned char TXBuffer[128];
- unsigned char TXCurrentPos = 0x00;
- unsigned char TXEndPos = 0x00;
- unsigned char TXBufferSize = 0x00;
- //if curpos == endpos, then the buffer is empty
- //after sending a byte, increment the curpos by 1
- //after adding a byte to the buffer, increment the endpos
- unsigned char ByteReceived = 0x00;
- // Set in the interrupt so it can be passed to main
- // After main reads it, main should clear it
- unsigned char countdown = 0;
- unsigned int time = 0x0000;
- unsigned char Switch_Count = 0;
- unsigned char waitingForACK = 0;
- unsigned char currentlySendingHandshake = 1; //If 1 then we're sending handshake
- //else we're sending data
- /** D E C L A R A T I O N S ******************************************/
- //creates a two dimensional array used to store the pins[pinA][data1][data2]
- //and their AD values. [pinB][data1][data2]
- unsigned char DATA_ARRAY[USED_AD_PINS][3];
- //size counts the size of the array we will be sending to the SD card
- //each time we do. This will be used in the handshake to tell the SD
- // module the number of bytes to expect. If we make any changes to the
- // packet scheme this will need to be changed.
- unsigned char size = USED_AD_PINS * 3;
- /** I N T E R R U P T S **********************************************/
- #pragma interrupt InterruptVectorHigh
- void InterruptVectorHigh(void){
- if(PIR1bits.RCIF == 1){
- TRISD = 0x00;
- LATDbits.LATD3 = ~LATDbits.LATD3;
- ByteReceived = RCREG;
- if(waitingForACK == 1){ //if we're waiting for ACK..
- if(ByteReceived == 0x06){ //and we get ACK
- currentlySendingHandshake = ~currentlySendingHandshake;
- if(currentlySendingHandshake == 1){
- countdown = 13;
- }
- else{
- countdown = 3;
- }
- if(TXBufferSize > 0){
- PIE1bits.TXIE = 1; //Finally reenable TX INT.
- } //if there is data to send
- waitingForACK = 0; //we're no longer waiting for ack
- }
- }
- }
- else if (PIR1bits.TXIF == 1){
- TRISD = 0x00;
- LATDbits.LATD2 = ~LATDbits.LATD2;
- if(TXBufferSize > 0){ // if there is data in the buffer
- TXBufferSize--; // update the amount of data in buffer
- TXREG = TXBuffer[TXEndPos]; // send next byte
- TXCurrentPos++; // update to the new position
- countdown--;
- }
- if(TXBufferSize == 0){
- PIE1bits.TXIE = 0; // If we run out of data in the buffer
- // pause TX
- }
- if(countdown == 0){
- waitingForACK = 1; //we're now waiting for ack
- PIE1bits.TXIE = 0; //stop sending data until we get ack
- }
- }
- }
- #pragma interruptlow InterruptVectorLow
- void InterruptVectorLow(void){
- }
- /** M A I N ***********************************************************/
- void main (void){
- uartINIT();
- rxEnable();
- globalInterruptINIT();
- TXInterruptINIT();
- RXInterruptINIT();
- Delay10KTCYx(50); //delay a while to give the SD card some time
- TXREG = 0x55; //initialize SD card
- while(ByteReceived != 0x06){ // wait for adknowledgement from SD
- }
- addToBuffer(0x74);
- addToBuffer(0x88);
- addToBuffer('A');
- addToBuffer(0x2E);
- addToBuffer(0x63);
- addToBuffer(0x73);
- addToBuffer(0x76);
- addToBuffer(0x00);
- addToBuffer(0x00);
- addToBuffer(0x00);
- addToBuffer(0x00);
- addToBuffer(0x03);
- addToBuffer(0x30);
- addToBuffer(0x31);
- addToBuffer(0x32);
- Delay1KTCYx(500);
- PIE1bits.TXIE = 1; //enable tx interrupt
- }
- void addToBuffer(char data){
- TXBuffer[TXEndPos] = data;
- TXEndPos++;
- TXBufferSize++;
- //if(waitingForACK == 0){
- //PIE1bits.TXIE = 1; //Enable the TX interrupt
- //} //If we're not waiting for ack
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement