/*******************************************************
Chip type : ATmega128A
Program type : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 1024
*******************************************************/
#include <mega128a.h>
#include <alcd.h>
#include <stdio.h>
#include <delay.h>
// Declare your global variables here
#define ON 1
#define OFF 2
#define NO 0
#define RIGHT 1
#define LEFT 2
////////////////////////
#define CS PORTB.0 //MCP3202칩 연결에 주의하세요!
#define DIN PINB.1
#define DOUT PORTB.2
#define CLK PORTB.3
//////////////////////////
unsigned int MCP3202(void)
{
unsigned char Count;
unsigned int ADdata=0;
// DIN ouput setting
CS=0;
DIN=1; //start bit
CLK=0;
CLK=1;
CLK=0;
DIN=1; // simgle mode
CLK=1;
CLK=0;
DIN=0; //ch 0
CLK=1;
CLK=0;
DIN=1;
CLK=1;
CLK=0;
// DOUT Input setting
CLK=1; //Start bit input NULL
CLK=0;
for(Count=0; Count <12; Count++) //for i2bit
// for(Count=0; Count <8; Count++)
{
ADdata=ADdata<<1;
CLK=1;
if(DOUT == 1) ADdata=ADdata|0x01;
CLK=0;
}
CS=1;
CLK=1;
return ADdata;
}
void main(void)
{
unsigned int ADval=0;
unsigned char test[17]=" ";
DDRA=PORTA=0xff;
DDRB=0xFD; PORTB=0xff;
DDRC=PORTC=0xff;
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("MCP3202");
while (1)
{
// Place your code here
ADval=MCP3202();
sprintf(test,"AD=%4u, V=%3.2fV ",ADval,(float)ADval/820);
lcd_gotoxy(0,1);
lcd_puts(test);
delay_ms(1000);
}
}