gcht101hello/code20240410.html 20240410
Sorce Code:P6MX313serial
hello

// P6MX18313serial 20240410
// description: show count read press 'c', clear count 
// board: Curiosity development PIC16F18313 OSC32MHz
// MPU; PIC16F18313 Flash:3.5KB, SRAM:256B, EEPROM:256B 
// DEVICE: USB-TTL ch340E C type
// TIMER:3, PWM10bit:2 USART:1,MSSP:1DSM;1DA5bit;1
// language: C XC8 v2.36
// IDE: MPLAB X v6.05, XC8 v2.41,PIC16F1xxx_DFP 1.17.343
// Debug Tool; Starter Kits (PKOB))
// PINOUT: PIC16F18313 dip8p
//3V3    1 VDD         VSS    8 GND
//LEDA5  2 RA5         RA0/TX 7 ICSPDAT >> USB-TTL RX
//BUTA4  3 RA4         RA1/RX 6 ICSPCLK << USB-TTL TX
//RST S1 4 MCR         RA2    5 LEDA2
//Jumper wire RA4:pin3 <<>> RC4/S1;pin6 button
//jumper wire RA0:pin7 <<>> RC5/d7:pin5 led
// * jumper PIN28:RA4/TX <<>> PIN10:RB7/USB/TX
// * jumper PIN29:RA0/RX/PIN29  <<>> PIN12:RB5/USB/RX
//----------------------------------------
//IO MAP
// RA5   RA4   RA3 RA2   RA1   RA0
// IO    IO    IO  IO    IO    IO
// OUT   IN    IN  OUT   IN    OUT
// LED5 BUTA4 RST  LED2  RX    TX 
//-------------------------------------
// board; breadboard 16G18313 5V INTOSC32MHz
// MPU: PIC16F18313  
// device: USB-TTL, LED
// debug tools: PICkit4
//--------------------------------------
// Curiosity development board
// D7   D6      D5      D4
// NC   LEDA2   NC      NC
#pragma config FEXTOSC = OFF, RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF, CSWEN = ON, FCMEN = ON      
#pragma config MCLRE = ON, PWRTE = OFF,WDTE = OFF,LPBOREN = OFF,BOREN = ON      
#pragma config BORV = LOW, PPS1WAY = ON, STVREN = ON,  DEBUG = OFF    
#pragma config WRT = OFF, LVP = ON    
#pragma config CP= OFF, CPD=OFF

#include 
#include   //for printf
#define _XTAL_FREQ 32000000 // delay_ms(x)

#define LEDA2    LATA2  //LED
#define LEDA5    LATA5  //LED
#define BUTA4    RA4    //LED
struct Codeinfo{
    char codeId[8];
    char function[8];
    char mcu[8];
    char version;
    long  date;   
};
struct Led{
    char status;
    char count;
};
struct Codeinfo ci={"P6MX313","blink","F18313",0,20240410};
struct Led le={0,0};
//------------------------------------
void ledCounter(void);
void putch(char ch);
void gpioBegin(void);
void serialBegin();
//------------------------------
void main(void) {
    gpioBegin();
    serialBegin();
    printf("\r\nP6MX18313serial\r\n");       
//----------------------------------------------------  
while(1){  
    ledCounter();
    LEDA2=(le.status==1)? 1:0;
    __delay_ms(50);
    LEDA5 = ! LEDA5;
    __delay_ms(50);
    printf("{\"codeId\":\"%s\",\"count\",%d}\r\n",
    ci.codeId,le.count);
    __delay_ms(500);
    if(RCIF){               // read serial 
        char readChar = RC1REG;    // read character
        if (readChar =='c'){       // if c, clear count
            le.count=0;  
            printf("clear count\r\n");
        }
    }    
    if(!BUTA4){
            printf("press button");
            __delay_ms(100);
    }      
}//end while
}//end main
void gpioBegin(void){
    ANSELA = 0b000000 ;   // digital  
    TRISA  = 0b011010 ;   // R1,RA3,RA4:input  RA0,RA2,RA5;output
    PORTA  = 0b000000 ;   // output:0
    WPUA   = 0b010000 ;   // RA4:PULLUP   
}    
    
void serialBegin(void){  
    TRISA0=0; // RA0/TX:0 output 
    TRISA1=1; // RA1/RX:1 input
    RC1STA = 0x90;//SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; 
    TX1STA = 0x24; //TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH h
    BAUD1CON = 0x08; // 
    SP1BRGH = 0x00;
    SP1BRG = 68;
    //RXPPS = 0x00;  //RA0->EUSART:RX     jumper PIN18   <<>> PIN10
    RXPPS = 0x01;    //RA1->EUSART:RX     jumper PIN18   <<>> PIN10
    RA0PPS= 0x14;    //RA0->EUSART:TX   jumper PIN18   <<>> PIN10
    //RA1PPS= 0x14;  //RA1->EUSART:TX   jumper PIN18   <<>> PIN10
    //RA2PPS= 0x14;  //RA2->EUSART:TX   jumper PIN17   <<>> PIN10
    //RA4PPS= 0x14;  //RA4->EUSART:TX   jumper PIN13   <<>> PIN10
    //RA5PPS= 0x14;  //RA5->EUSART:TX     jumper PIN2   <<>> PIN10
  //----------------------------------------  
}

void ledCounter(void){
    le.count++;
    le.status=le.count%2;
}
void putch(char ch) {
    while (!TXIF);      //print
    TX1REG = ch;
}
/*
result 
11:01:39.537 -> {"codeId":"P6MX313","count",5}
11:01:40.007 -> clear count
11:01:40.100 -> {"codeId":"P6MX313","count",1}
 */

/*
 data: 115/256bytes 45%
 program: 1041/2048bytes 51%
 */