In week 11 we had the task to measure something with an microcontroller. Well, we also should create
our one board for this, so i used the fab-pic again. Lucky for you ;)
This way you will get some code-snipletts more for your first steps in the PIC-World...
My first idea was to realise a kind of touch-screen.
The theory behind this is a capacitive-touch-sensor, made out of a piece of FR4 or any other metal and a resistor.
The sensor (the piece of metal) is connected with an input-pin equiped with a schmitt-trigger. The sensor is also
connected to ground by a very high resistor, 1 mega-ohm by example.
The touch-sense itself is all made by the software in a loop:
temp = 0;
for(i=0; i<5; i++)
{
TRISCbits.TRISC0 = 0; //Set B0 to output
PORTCbits.RC0 = 1; //load the sensor-element
__delay_ms(1); //wait for the cap to load
WriteTimer1(0x0); //Reset Timer 1
T1CONbits.TMR1ON = 1; //Start Timer1
TRISCbits.TRISC0 = 1; //Set C0 to to input
while (PORTCbits.RC0 == 1); //wait for discharge
T1CONbits.TMR1ON = 0; //Stop Timer1
temp = temp + ReadTimer1();
__delay_ms(1);
}
button_1 = temp / 5;
Open1USART //Init USART 1
(USART_TX_INT_OFF & //Send-Interrupt off
USART_RX_INT_OFF & //Receivce-Interrupt off
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 16); //Baud 16 => 115.200
//needed by printf, used to configure the destination
void putch(char data) {
while( ! TXIF) // check buffer
continue; // wait till ready
TXREG = data; // send data
}
printf("This is Fab-Pic!\n"); //Say hello to the world!
while (PIR1bits.RC1IF != 1); //wait until USART received data
some_number = getc1USART(); //read in the received char
ANCON1 = 0b00000000; // Just AN0 is an analog input, all other pins are digital
ADCON1 = 0b00000000; //No special triggers, Ref is Vdd and Vss
ADCON2 = 0b10111100; //result is right justified, 8 Tads, Clock = 32Mhz / 4
ADCON0 = 0b00000001; //Select AD0 for all AD-Operations
ADCON0 = 0b00000001; //Select AD-Channel 0
ConvertADC(); //start AD-Conversion
while(BusyADC()); //wait for conversion to finish
adc_result = ((ADRESH)<<8)|(ADRESL); //read in the result
adc_result = (adc_result * 1.22); //make mV out of the adc-value
// I2C-Init
OpenI2C(MASTER, SLEW_OFF); // Initialize I2C module
SSPADD = 19; //100kHz = 79, 400khz = 19
// Routine for readout of ADC MCP3221 by I2C
unsigned int adchanger(void) {
unsigned int highbyte, lowbyte, adc;
highbyte = lowbyte = adc = 0;
StartI2C(); //Send a start-condition
while (SSPCON2bits.SEN); //Wait for finishing the start-condition
WriteI2C(0b10011011); //Send the MCP the command to start a conversion
while (SSPCON2bits.ACKSTAT); //Wait for the ACK from the ADC
highbyte = ReadI2C(); //Read in the highbyte
IdleI2C(); //wait the readout to finish
AckI2C(); //send a ACK to the ADC
IdleI2C(); //wait for finish
lowbyte = ReadI2C(); //read in the lowbyte
IdleI2C(); //Wait for finisch
StopI2C(); //Send the stop-condition
while (SSPCON2bits.PEN); //Wait for finishing the stop-condition
highbyte = (highbyte << 8); //Converse the High and Lowbyte into one unsigned int
adc = (highbyte | lowbyte);
return (adc);
}