sshahryiar
Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh
|
MCP3201 12-bit SPI ADC Driver |
Posted: Mon Apr 08, 2013 11:15 pm |
|
|
Code: |
#include <16F87.h>
#device *= 16
#fuses NOWDT, INTRC, PROTECT, CPD, PUT, NOWRT
#fuses NOMCLR, BROWNOUT, FCMEN, NOIESO, NOLVP
#use delay (clock = 4MHz)
#use SPI(MASTER, DI = PIN_A1, CLK = PIN_A2, BITS = 16, MODE = 0)
#define CS pin_A0
#include <lcd420.c>
void setup();
void main()
{
register float v = 0.0;
register unsigned long adc_value = 0;
setup();
while(TRUE)
{
output_low(CS);
adc_value = spi_xfer(0);
output_high(CS);
adc_value &= 0x1FFE;
adc_value >>= 1;
v = (adc_value * 5.0);
v /= 4095.0;
lcd_gotoxy(1,1);
printf(lcd_putc, "V: %1.3f Volt ", v);
lcd_gotoxy(1,2);
printf(lcd_putc, "ADC: %4lu ", adc_value);
delay_ms(100);
};
}
void setup()
{
setup_oscillator(OSC_4MHZ);
disable_interrupts(global);
setup_WDT(WDT_off);
setup_comparator(NC_NC_NC_NC);
setup_CCP1(CCP_off);
setup_SPI(SPI_disabled);
setup_timer_0(0 | 0 | 0);
setup_timer_1(T1_disabled);
setup_timer_2(T2_disabled, 255, 1);
set_timer0(0);
set_timer1(0);
set_timer2(0);
output_high(CS);
lcd_init();
lcd_putc("\f");
delay_ms(100);
} |
_________________ https://www.facebook.com/MicroArena
SShahryiar |
|