Professional Documents
Culture Documents
* File: main.c
* Author: Charles M Douvier
* Contact at: http://iradan.com
*
* Created on January 18, 2014, 9:42 AM
*
* Target Device:
* 16F1509 on Tautic 20 pin dev board
*
* Project:
* A/D --> LCD Test
* 8-bit resolution across Vdd to Vref (0-5V)
* for 3.3V operation adjust A/D math
*
* LCD (44780 type) Test with XC8 compiler
* LCD code ported from Mike Pearce's 2001 LCD code for HI-TECH C
* as found on http://www.microchipc.com/
*/
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <plib.h>
//config bits
#pragma config FOSC=INTOSC, WDTE=OFF, PWRTE=OFF, MCLRE=ON, CP=OFF, BOREN=ON,
CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF
#pragma config WRT=OFF, STVREN=OFF, LVP=OFF
/*
* LCD RS LATA.5
* LCD EN LATA.4
* LCD DATA4 LATC.0
* LCD DATA5 LATC.1
* LCD DATAT6 LATC.2
* LCD DATA7 LATC.3
* LED LATA.0 for scan rate/heartbeat
*/
/*
* Clear and home the LCD
*/
void lcd_clear(void)
{
LATAbits.LATA5 = 0;
lcd_write(0x1);
__delay_ms(2);
}
/*
* Go to the specified position
*/
/*
* Write 16 spaces on LCD 2 to avoid blanking, (ugly CLEAR effect)
* this is slow but work for my needs
*/
void lcd_clrline2(void)
{
lcd_goto(40);
lcd_puts(" ");
lcd_goto(40);
}
void lcd_init(void)
{
LATAbits.LATA5 = 0; // write control bytes
LATC = 0x03;
__delay_ms(150); //power on delay
lcd_strobe();
__delay_ms(5);
lcd_strobe();
__delay_ms(5);
lcd_strobe();
__delay_ms(5);
LATC = 0x02; // set 4 bit mode
__delay_ms(5);
lcd_strobe();
__delay_ms(5);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x08); // display off
lcd_write(0x0C); // display on cursor+blink off
lcd_write(0x06); // entry mode
}
int main(void) {
// set up oscillator control register, using internal OSC at 4MHz.
OSCCONbits.IRCF = 0x0d; //set OSCCON IRCF bits to select OSC frequency 4MHz
OSCCONbits.SCS = 0x02; //set the SCS bits to select internal oscillator block
TRISCbits.TRISC0 = 0; // output
TRISCbits.TRISC1 = 0; // output
TRISCbits.TRISC2 = 0; // output
TRISCbits.TRISC3 = 0; // output
TRISAbits.TRISA0 = 0; // output
TRISAbits.TRISA4 = 0; // output
TRISAbits.TRISA5 = 0; // output
TRISCbits.TRISC7 = 1; //analog input
ANSELCbits.ANSC7 = 1; //...setup on PORTC.7/AN9
LATAbits.LATA0 = 0; //LED Im-Alive test
lcd_init();
__delay_ms(10);
lcd_clear();
/* ADCON1
* bit 7 ADFM: ADC Result Format Select bit
* 0 = Left justified. Six Least Significant bits of ADRESL are set to ?0? when the
conversion result is loaded.
* bit 6-4 ADCS<2:0>: ADC Conversion Clock Select bits
* 110 = FOSC/64
* bit 3-2 Unimplemented: Read as ?0?
* bit 1-0 ADPREF<1:0>: ADC Positive Voltage Reference Configuration bits
* 00 = VREF+ is connected to VDD
*/
ADCON1 = 0b01100000; //left justified, FOSC/64 speed Vref=Vdd
while(1)
{
LATAbits.LATA0 = 0; //debugging
lcd_clrline2(); //clear LCD line 2 by writting " " and return
__delay_us(5);
GO = 1;
while (GO) continue; //wait for conversion
an9_value = ADRESH; //AN9 value