* P6MX313serialadc 20240501
* description: read ADC on POT, serial print
* board: Curiosity development PIC16F18313
* MPU; PIC6F18313 DIP8P OSC32
* Flash:2KW, RAM:256B
* IDE: MPLABX v6.05, XC8 v2.41
* Serial; monitor 115200baud using Arduino IDE
//
PIC16F18313 DIP 8P Pin pinout
3V3 1 VDD VSS 8 GND
- 2 RA5 RA0/RX 7 ICSPDAT << USB-TTL TX
- 3 RA4 RA1/TX 6 ICSPCLK >> USB-TTL RX
RST 4 MCLR/RA3 RA2 5 POT
* jumper RA1/TX/PINIB >> RB7/TX/PIN10 USB-TTL RX GREEN
* jumper RA0/RX/PiN19 << RB5/RX/PIN12 USB-TTL TXYELLOW
* jumper RA2/PIN17 << RC0 PIN16 POT 10kohm
* //------------------------------------*/
#pragma config FEXTOSC = OFF,RSTOSC = HFINT32 // HFINTOSC (32MHz)
#pragma config CLKOUTEN = OFF,CSWEN = OFF,FCMEN = OFF
#pragma config MCLRE = ON,PWRTE = OFF,WDTE = OFF,LPBOREN = OFF
#pragma config BOREN = OFF,BORV = LOW,PPS1WAY = OFF,STVREN = ON
#pragma config DEBUG = OFF
#pragma config WRT = OFF,LVP = ON,CP = OFF,CPD = OFF
#include
#include //for printf
#define _XTAL_FREQ 32000000L // for __delay_ms(x)
struct Codeinfo{
char codeId[9];
char function[16];
char mpu[16];
uint8_t var;
char date[9];
};
struct Adc{
unsigned char value;
unsigned char count;
char valueStr[3];
char countStr[3];
};
struct Timer0{
unsigned int count;
unsigned char flag;
};
struct Codeinfo ci={"P6MX313\0","serialadc\0","16F18313",1,"20240501\0"};
struct Adc ad={0,0,"xx\0","xx\0"};
struct Timer0 t0={ 49911,0};
char hexTable[17]={"0123456789ABCDEF"};
//------------------------------------------
void putch(char ch);//Transmits a single character through the serial port.
void serialPrint(char *string);//Prints a string through the serial port.
void gpioBegin(void); //Initializes Input/Output (GPIO) pins
void serialBegin(void); //Configures the USART module
void adcBegin(void); //Configures the ADC module
void timer0Begin(void); //Configures the USART module
// ******************* main ********************
void main() {
gpioBegin();
serialBegin();
adcBegin();
timer0Begin();
PEIE = 1 ; //
GIE = 1 ; //
putch('\n');
serialPrint(ci.codeId);
serialPrint(ci.function); putch('\n');
serialPrint(ci.mpu); putch('\n');
serialPrint(ci.date); putch('\n');
// ----------- --------------------------------------------
while(1){
if (t0.flag == 0){ // wait timer0
t0.flag=1;
ad.count=(ad.count+1)%60;
ad.countStr[0]=0x30+ad.count/10;
ad.countStr[1]=0x30+ad.count%10;
serialPrint("{\"count\":");
serialPrint(ad.countStr);
GO = 1; // start ADC
while(GO){}; // wait ADC
serialPrint(",\"adc\":0x");
ad.valueStr[0]=hexTable[ADRESH/16];
ad.valueStr[1]=hexTable[ADRESH%16];
serialPrint(ad.valueStr);
serialPrint("}\n");
}//end if
}//end while
}//end main
//--------------------------------------------
void __interrupt() isr(void){
if (TMR0IF == 1) { //
TMR0IF = 0; //
TMR0H = t0.count >> 8; //
TMR0L = t0.count & 0x00FF;
if (t0.flag == 0) t0.flag = 1;
else t0.flag = 0;
}
}
void gpioBegin(void){
TRISA = 0b001101; // RA0,RA2.RA3:input
ANSELA = 0b000100; // RA2 :analog, other:digital
}
void serialBegin(void){
ANSA1=0; // RA1:OUTPUT
ANSA0=1; // RA0:INPUT
RC1STA = 0x90;//SPEN ; RX9 8-bit; CREN ; ADDEN disabled; SREN disabled;
TX1STA = 0x24; //TX9 8-bit; TX9D 0; SENDB; TXEN enabled; SYNC; BRGH h
BAUD1CON = 0x08; //
SP1BRGH = 0x00;
SP1BRG = 68;
RXPPS = 0x00; //RA0->EUSART:RX; << USB-TTL TX
RA1PPS= 0x14; //RA1->EUSART:TX; >> USB-TTL TX
//RA4PP=0x14; //RA4:TX //RA5PPS=0x14; //RA5:TX //RA2PPS=0x14; //RA2:TX
}
void adcBegin(void){
ANSA2 = 1; // ANSA2(RA2) input
ADACT = 0b00000000; // AD
ADCON1 = 0b00000000; // AD Fosc/2 // ref VDD
ADCON0 = 0b00001001; // ADC ON ed. // 000010 = ANA2
}
void timer0Begin(void){
//T0CON1 = 0b01000111 ; //1:128 Prescaler Rate Select bit
T0CON1 = 0b01001001 ; //1:512 Prescaler Rate Select bit
T0CON0 = 0b00010000 ; //
TMR0H = t0.count >> 8 ; //
TMR0L = t0.count & 0x00FF ; //
TMR0IF = 0 ; //
TMR0IE = 1 ; //
T0EN = 1 ; // TIMER0?????
t0.flag = 0 ;
}
void putch(char ch) {
while (!TXIF); //print
TX1REG = ch;
}
void serialPrint(char *string){
while(*string) putch(*string ++); //unit 00 //pointer++
}
/*
Data: 93/256 bytes 36%
Program: 616/2048bytes 30%
*/
/*result
07:19:35.344 -> P6MX313serialadc
07:19:35.344 -> 16F18313
07:19:35.344 -> 20240501
07:19:35.344 -> {"count":01,"adc":0xFB}
07:19:36.369 -> {"count":02,"adc":0xFB}
07:19:37.353 -> {"count":03,"adc":0xFA}
*/