Update Call

This commit is contained in:
Bruno Rybársky 2024-05-30 00:19:44 +02:00
parent bcc466f985
commit 293060dac0

121
main.c

@ -1,9 +1,9 @@
/*
* File: main.c
* Author: Bruno Rybársky
*
* Created on November 17, 2022, 12:49 PM
*/
* File: main.c
* Author: Bruno Rybársky
*
* Created on November 17, 2022, 12:49 PM
*/
//PINS
//GP0 - Button 1
@ -104,20 +104,19 @@
/* These are the definitions */
const char String1[3] = "OK"; //ok response
const char String2[11] = "NO CARRIER"; //call state change
const char String3[6] = "ERROR"; //ok response
const char String2[27] = "+CLIP: \"+421905708125\",145"; //call state change
const char String4[10] = "SMS Ready"; //modem startup
const char StringDial[14] = "+421905708125";
//#define TMR0_BAUD (256-133) /* trial and error value, comes to 3603 Baud */
#define TMR0_BAUD (256-133) /* trial and error value, comes to 3603 Baud */
#define TMR0_BAUD (256-128) /* trial and error value, comes to 3603 Baud */
#define TxBUF_SIZE 32
#define RxBufSize 8
volatile char TxBuf[TxBUF_SIZE] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
#define TxBUF_MASK (TxBUF_SIZE-1)
volatile char RxBuf[RxBufSize];
volatile char RxIPtr;
@ -136,6 +135,7 @@ volatile char TxCount;
volatile char RxFlags;
volatile char RxBufDiff;
volatile char InChar;
volatile char turnOffCounter = 0;
//start parsing serial data:
@ -166,14 +166,15 @@ volatile char Millis = 0;
/* processor). When a character has been added, the "new data" flag is */
/* set to tell the interrupt routine to restart sending data. */
/* Note: data can be added to the buffer faster than the interrupt */
/* routine can transmit it */
void SendChar(char c) {
GIE = 0; /* disable interrupts to avoid a race condition */
GIE = 0; /* disable interrupts to avoid a race condition */
TxBuf[TxOPtr++] = c;
TxOPtr = TxOPtr & TxBUF_MASK;
TxFlags |= 0x80;
GIE = 1; /* safe to restart them now */
GIE = 1; /* safe to restart them now */
}
void SendString(const char *c) {
@ -204,15 +205,15 @@ void __interrupt()isr(void) {
// if millis overflows
// Debouncing code sampling GPIO2 only runs when Millis overflows
//if ((GPIOtemp & 0x0D) != (GPIO & 0x0D)) { // compare current input with last round
if ((GPIO & 0x03) != GPIOold){
if ((GPIO & 0x03) != GPIOold) {
GPIOtemp = (GPIO & 0x03) ^ GPIOold;
GPIOold = GPIO & 0x03;
// GPIO2 changed, perform debouncing
if (GPIOtemp & 0x01) {
if (!(GPIO & 0x01)) {
GPIOdeb |= (1 << 4); // arm
}
}
else {
GPIOdeb |= (1 << 5); // disarm
}
@ -221,15 +222,17 @@ void __interrupt()isr(void) {
if (GPIOtemp & 0x02) {
if (!(GPIO & 0x02)) {
GPIOdeb |= (1 << 6); // arm
}
}
else {
GPIOdeb |= (1 << 7); // disarm
}
}
}
if (turnOffCounter > 0 && !--turnOffCounter) {
GPIO &= 0xFD;
}
}
// Routine to receive asynchronous data from GPIO5
// credit: http://www.electro-tech-online.com/micro-controllers/18828-finding-serial-start-bit-bit-banging.html#post117227
asm("decfsz _RecSkips,F");
@ -246,8 +249,8 @@ void __interrupt()isr(void) {
asm("goto DoneRS232_Rx");
asm("movf _InByte,W");
asm("movwf _RxChar");
asm("bsf _RxFlags,1"); // b_byte_available
asm("bcf _RxFlags,0"); // b_receiving
asm("bsf _RxFlags,1"); // b_byte_available
asm("bcf _RxFlags,0"); // b_receiving
RxBuf[RxIPtr++] = RxChar; /* write received byte to circular buffer */
RxIPtr = RxIPtr & 0x7;
asm("goto DoneRS232_Rx");
@ -259,7 +262,7 @@ void __interrupt()isr(void) {
asm("goto DoneRS232_Rx");
asm("movlw 4");
asm("movwf _RecSkips");
asm("bsf _RxFlags,0"); // b_receiving
asm("bsf _RxFlags,0"); // b_receiving
asm("movlw 80h");
asm("movwf _InByte");
asm("DoneRS232_Rx:");
@ -270,34 +273,34 @@ void __interrupt()isr(void) {
// Here check if there is data being sent, or ready to send
asm("btfsc _TxFlags, 6"); /* already sending ?*/
asm("goto Tx_Sending");
asm("btfsc _TxFlags, 7"); // data to send
asm("btfsc _TxFlags, 7"); // data to send
asm("goto Tx_Start_Bit");
asm("incf _TxSkips, F"); // set back to 1, so we retest next time round
asm("goto DoneRS232_Tx"); // nothing to do
asm("incf _TxSkips, F"); // set back to 1, so we retest next time round
asm("goto DoneRS232_Tx"); // nothing to do
// come here to start sending a new byte
asm("Tx_Start_Bit:");
asm("decfsz _TxSkips, f");
asm("goto DoneRS232_Tx");
asm("bcf GPIO, 4"); // hardware start bit
asm("bcf GPIO, 4"); // hardware start bit
TxChar = TxBuf[TxIPtr++];
TxIPtr = TxIPtr & TxBUF_MASK;
TxFlags = 8; // initialise bit count, clear FULL flag
asm("bsf _TxFlags, 6"); // indicate we are sending data
asm("bsf _TxFlags, 6"); // indicate we are sending data
TxSkips = 3;
asm("goto DoneRS232_Tx");
//here if we are already in the process of sending data
asm("Tx_Sending:");
asm("decfsz _TxSkips, F"); // only waggle o/p every third intr.
asm("decfsz _TxSkips, F"); // only waggle o/p every third intr.
asm("goto DoneRS232_Tx");
asm("btfsc _TxFlags, 5"); // time to send stop bit?
asm("btfsc _TxFlags, 5"); // time to send stop bit?
asm("goto Tx_Stop_Bit");
asm("bcf STATUS, 0"); // carry bit
asm("bcf STATUS, 0"); // carry bit
asm("rrf _TxChar, f");
asm("btfss STATUS, 0");
asm("goto Tx_Set_Bit");
@ -308,13 +311,13 @@ void __interrupt()isr(void) {
// here we have sent the data bit, decrement the bit counter
asm("Tx_Sent_Bit:");
asm("movlw 3");
asm("movwf _TxSkips"); // reset Tx skip counter for 3 more intrs
asm("decf _TxFlags, F"); // bit count in lower 3 bits
asm("movwf _TxSkips"); // reset Tx skip counter for 3 more intrs
asm("decf _TxFlags, F"); // bit count in lower 3 bits
asm("movf _TxFlags, W");
asm("andlw 7");
asm("btfss STATUS, 2"); // if result is zero
asm("btfss STATUS, 2"); // if result is zero
asm("goto DoneRS232_Tx");
asm("bsf _TxFlags, 5"); // all data sent, sent stop bit next time
asm("bsf _TxFlags, 5"); // all data sent, sent stop bit next time
asm("goto DoneRS232_Tx");
// here to send the stop bit and check if there''s more data
@ -323,15 +326,15 @@ void __interrupt()isr(void) {
asm("bsf GPIO, 4");
asm("movlw 3");
asm("movwf _TxSkips");
asm("bcf _TxFlags, 6"); // sending_data flag
asm("bcf _TxFlags, 5"); // send_stop_bit flag
asm("bcf _TxFlags, 6"); // sending_data flag
asm("bcf _TxFlags, 5"); // send_stop_bit flag
TxCount = 1 + TxOPtr - TxIPtr;
asm("decfsz _TxCount, F"); // counter of number of chars still to send
asm("decfsz _TxCount, F"); // counter of number of chars still to send
asm("goto Tx_More");
asm("goto DoneRS232_Tx"); // all sent
asm("goto DoneRS232_Tx"); // all sent
asm("Tx_More:");
asm("bsf _TxFlags, 7"); // signify more data to send
asm("bsf _TxFlags, 7"); // signify more data to send
asm("DoneRS232_Tx:");
@ -356,28 +359,25 @@ void init(void) {
OSCCONbits.IRCF2 = 1;
TRISIObits.TRISIO5 = 1;
TRISIObits.TRISIO4 = 0;
TRISIObits.TRISIO1 = 1;
TRISIObits.TRISIO1 = 0;
TRISIObits.TRISIO0 = 1;
WPUbits.WPU1 = 1;
WPUbits.WPU0 = 1;
ADCON0bits.ADON = 0;
ANSELbits.ANS0 = 0;
ANSELbits.ANS1 = 0;
ANSELbits.ANS2 = 0;
ANSELbits.ANS3 = 0;
GPIObits.GP1 = 0;
}
int startSMS() {
void startSMS(const int flagBit) {
if (FlagC & (1 << 3)) {
SendString("AT+CMGS=\"");
SendString(StringDial);
SendString("\"\n");
Flag3 |= (1 << 5);
return 1;
Flag4 |= (1 << flagBit);
}
return 0;
}
void processString(const char *string, volatile char *inputCharacter, const int flagBit) {
@ -388,6 +388,11 @@ void processString(const char *string, volatile char *inputCharacter, const int
if (flagBit == 3) {
SendString("AT+CMGF=1\r\n");
}
if (flagBit == 1) {
GPIO |= 0x02;
turnOffCounter = 100;
SendString("ATH\n");
}
}
if (*inputCharacter != string[messagePointer]) {
Flag2 &= ~(1 << flagBit);
@ -398,9 +403,7 @@ void processString(const char *string, volatile char *inputCharacter, const int
void handleGPIO(const int pin, const int flagBit) {
if (GPIOdeb & (1 << pin)) {
GPIOdeb &= ~(1 << pin);
if(startSMS()){
Flag4 |= (1 << flagBit);
}
startSMS(flagBit);
}
}
@ -408,11 +411,8 @@ void main(void) {
init();
SendChar('\n');
while (1) {
handleGPIO(6, 0);
handleGPIO(7, 1);
handleGPIO(4, 2);
handleGPIO(5, 3);
handleGPIO(4, 0);
handleGPIO(5, 1);
if (RxIPtr != RxIPtr_old) {
if (RxIPtr < RxIPtr_old) {
@ -430,25 +430,15 @@ void main(void) {
if (Flag3 & (1 << 5)) {
if (Flag4 & (1 << 0)) {
SendString("1 ON");
SendString("ON");
Flag4 &= ~(1 << 0);
}
if (Flag4 & (1 << 1)) {
SendString("1 OFF");
SendString("OFF");
Flag4 &= ~(1 << 1);
}
if (Flag4 & (1 << 2)) {
SendString("2 ON");
Flag4 &= ~(1 << 2);
}
if (Flag4 & (1 << 3)) {
SendString("2 OFF");
Flag4 &= ~(1 << 3);
}
SendChar(26);
Flag3 &= ~(1 << 5);
}
@ -456,7 +446,6 @@ void main(void) {
processString(String1, &InChar, 0);
processString(String2, &InChar, 1);
processString(String3, &InChar, 2);
processString(String4, &InChar, 3);
messagePointer++;
@ -465,4 +454,4 @@ void main(void) {
RxIPtr_old = RxIPtr;
}
}
}
}