Estou tentando usar o ADC do atmega8 e não estou conseguindo, se alguem poder ajudar eu serei grato.
ATMega8 PIN 23 > PIN 2 do LM35
ATMega8 PIN AVREF > +5V
ATMega8 PIN AVCC > +5V
Segue codigo em java. e logo abaixo o codigo convertido pra C.
- Código: Selecionar todos
import mcujavasource.mcu.*;
import mcujavasource.mcu.external.lcd.CharacterLcd;
/** Tutorial 9: Character LCD.
*/
public class Main extends Microcontroller
{
private static final int REFERENCE_VOLTAGE = 5000;
private int pos = 0;
private Adc adc;
private Timer timer;
private CharacterLcd lcd;
private Port lcdPort = getHardware().getPort("D");
private Port lcdControlPort = getHardware().getPort("C");
private Pin rsPin = lcdControlPort.getPin(5);
private Pin ePin = lcdControlPort.getPin(3);
private Pin DATA_3 = getHardware().getPort("D").getPin(7);
private Pin DATA_2 = getHardware().getPort("D").getPin(6);
private Pin DATA_1 = getHardware().getPort("D").getPin(5);
private Pin DATA_0 = getHardware().getPort("D").getPin(4);
@Override
public void init()
{
getHardware().setAllPortsDirection(Pin.IN);
getHardware().setAllPortsPullUp(true);
lcd = new CharacterLcd(16, 2, rsPin, ePin, lcdPort, 4);
//ADC
adc = getHardware().getAdc();
adc.setInput(0);
adc.setPrescaling(8);
adc.setVoltageReference(Adc.VoltageReference.AREF);
VoltageEvent voltageEvent = new VoltageEvent();
adc.addAdcListener(voltageEvent);
adc.setConversionCompletedFired(true);
adc.setEnabled(true);
timer = getHardware().getDefaultTimer(16,1);
timer.setMode(TimerMode.CTC);
timer.setPrescaling(1024);
timer.setEnabled(true);
TimerCompare compare = timer.getOutputCompare("A");
// divide 976 para 1Mhz, 7812 para 8Mhz
compare.setValue(976);
TimerHandler timerHandler = new TimerHandler();
compare.addTimerCompareListener(timerHandler);
compare.setCompareMatchFired(true);
}
@Override
public void start()
{
lcd.init();
getHardware().setInterruptsEnabled(true);
}
public void sendCommand(int cmd){
rsPin.setOutput(Pin.LOW);
ePin.setOutput(Pin.LOW);
writeDatai(cmd);
}
public void sendData(char dat){
rsPin.setOutput(Pin.HIGH);
ePin.setOutput(Pin.LOW);
writeData(dat);
}
public void sendDatai(int dat){
rsPin.setOutput(Pin.HIGH);
ePin.setOutput(Pin.LOW);
writeDatai(dat);
}
public void writeDatai(int dat){
if((dat & 0x80) == 0x80) DATA_3.setOutput(Pin.HIGH);//pino
else DATA_3.setOutput(Pin.LOW);
if((dat & 0x40) == 0x40) DATA_2.setOutput(Pin.HIGH);//pino
else DATA_2.setOutput(Pin.LOW);
if((dat & 0x20) == 0x20) DATA_1.setOutput(Pin.HIGH);//pino
else DATA_1.setOutput(Pin.LOW);
if((dat & 0x10) == 0x10) DATA_0.setOutput(Pin.HIGH);//pino
else DATA_0.setOutput(Pin.LOW);
ePin.setOutput(Pin.HIGH);
getHardware().getDelay().sleep(60);
ePin.setOutput(Pin.LOW);
if((dat & 0x08) == 0x80) DATA_3.setOutput(Pin.HIGH);//pino
else DATA_3.setOutput(Pin.LOW);
if((dat & 0x40) == 0x04) DATA_2.setOutput(Pin.HIGH);//pino
else DATA_2.setOutput(Pin.LOW);
if((dat & 0x02) == 0x02) DATA_1.setOutput(Pin.HIGH);//pino
else DATA_1.setOutput(Pin.LOW);
if((dat & 0x01) == 0x01) DATA_0.setOutput(Pin.HIGH);//pino
else DATA_0.setOutput(Pin.LOW);
ePin.setOutput(Pin.HIGH);
getHardware().getDelay().sleep(60);
ePin.setOutput(Pin.LOW);
getHardware().getDelay().sleep(60);
}
public void writeData(char dat){
if((dat & 0x80) == 0x80) DATA_3.setOutput(Pin.HIGH);//pino 13
else DATA_3.setOutput(Pin.LOW);
if((dat & 0x40) == 0x40) DATA_2.setOutput(Pin.HIGH);//pino 13
else DATA_2.setOutput(Pin.LOW);
if((dat & 0x20) == 0x20) DATA_1.setOutput(Pin.HIGH);//pino 13
else DATA_1.setOutput(Pin.LOW);
if((dat & 0x10) == 0x10) DATA_0.setOutput(Pin.HIGH);//pino 13
else DATA_0.setOutput(Pin.LOW);
ePin.setOutput(Pin.HIGH);
getHardware().getDelay().sleep(60);
ePin.setOutput(Pin.LOW);
if((dat & 0x08) == 0x80) DATA_3.setOutput(Pin.HIGH);//pino 13
else DATA_3.setOutput(Pin.LOW);
if((dat & 0x40) == 0x04) DATA_2.setOutput(Pin.HIGH);//pino 13
else DATA_2.setOutput(Pin.LOW);
if((dat & 0x02) == 0x02) DATA_1.setOutput(Pin.HIGH);//pino 13
else DATA_1.setOutput(Pin.LOW);
if((dat & 0x01) == 0x01) DATA_0.setOutput(Pin.HIGH);//pino 13
else DATA_0.setOutput(Pin.LOW);
ePin.setOutput(Pin.HIGH);
getHardware().getDelay().sleep(60);
ePin.setOutput(Pin.LOW);
getHardware().getDelay().sleep(60);
}
public void lcdInit(){
sendCommand(1);
getHardware().getDelay().sleep(30);
sendCommand(40);
sendCommand(28);
sendCommand(8);
getHardware().getDelay().sleep(30);
//sendCommand(6);
// sendCommand(13);
}
private class VoltageEvent implements AdcListener{
@Override
public void conversionCompleted() {
lcd.setPosition(0, 0);
lcd.write("ADC: ");
/*pego a valor do adc. Usando isso pra verificar o valor lido 0 to 1023?
* esta sempre saindo "p"*/
int v_1 = adc.getValue();//se eu mudar pra getInput() o retorno é 0 e não p.
sendDatai(v_1 + 0x30);
lcd.setPosition(1, 0);
lcd.write("ADC Value:");
/* O resultado é 0.00 e isso eu não entendo o porque*/
int v_2 = v_1 * REFERENCE_VOLTAGE / 1024;
sendDatai((v_2 / 1000) + 0x30);
lcd.write(".");
sendDatai(((v_2 % 1000)/100) + 0x30);
sendDatai(((v_2 % 100)/10) + 0x30);
sendDatai((v_2 % 10) + 0x30);
}
}
private class TimerHandler implements TimerCompareListener{
@Override
public void compareMatched() {
adc.setBusy(true);
}
}
}
-----------------------------------------------------------------------------------
C
- Código: Selecionar todos
/* This file is automatically generated by McuJavaSource application
from java source file.
Do not edit this file manually. Edit java source file instead.
Target platform: avr
Target device: atmega8
*/
#define true !0
#define false 0
typedef unsigned char boolean;
typedef unsigned char byte;
#define REFERENCE_VOLTAGE 5000
void CharacterLcd_init(void);
void CharacterLcd_sendCommand(byte data);
void CharacterLcd_ConnectionModePort4bits_sendByte(byte data);
void CharacterLcd_sendData(byte data);
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include "mcujavasource/mcu/external/lcd/nativeSource/CharacterLcd_writeString.c"
int pos = 0;
void init(void)
{ OCR1A = 976;
DDRC = _BV(PC3) | _BV(PC5);
DDRD = _BV(PD4) | _BV(PD5) | _BV(PD6) | _BV(PD7);
PORTB = _BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5) | _BV(PB6) | _BV(PB7);
PORTC = _BV(PC0) | _BV(PC1) | _BV(PC2) | _BV(PC4) | _BV(PC6);
PORTD = _BV(PD0) | _BV(PD1) | _BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5) | _BV(PD6) | _BV(PD7);
ADCSRA = _BV(ADPS0) | _BV(ADPS1) | _BV(ADIE) | _BV(ADEN);
TCCR1B = _BV(WGM12) | _BV(CS10) | _BV(CS12);
TIMSK = _BV(OCIE1A);
}
void start(void)
{ CharacterLcd_init();
sei();
}
void sendCommand(int cmd)
{ PORTC &= ~_BV(PC5);
PORTC &= ~_BV(PC3);
writeDatai(cmd);
}
void sendData(char dat)
{ PORTC |= _BV(PC5);
PORTC &= ~_BV(PC3);
writeData(dat);
}
void sendDatai(int dat)
{ PORTC |= _BV(PC5);
PORTC &= ~_BV(PC3);
writeDatai(dat);
}
void writeDatai(int dat)
{ if((((dat & 0x80)) == 0x80)) PORTD |= _BV(PD7);
else PORTD &= ~_BV(PD7);
if((((dat & 0x40)) == 0x40)) PORTD |= _BV(PD6);
else PORTD &= ~_BV(PD6);
if((((dat & 0x20)) == 0x20)) PORTD |= _BV(PD5);
else PORTD &= ~_BV(PD5);
if((((dat & 0x10)) == 0x10)) PORTD |= _BV(PD4);
else PORTD &= ~_BV(PD4);
PORTC |= _BV(PC3);
_delay_loop_2(15000);
PORTC &= ~_BV(PC3);
if((((dat & 0x08)) == 0x80)) PORTD |= _BV(PD7);
else PORTD &= ~_BV(PD7);
if((((dat & 0x40)) == 0x04)) PORTD |= _BV(PD6);
else PORTD &= ~_BV(PD6);
if((((dat & 0x02)) == 0x02)) PORTD |= _BV(PD5);
else PORTD &= ~_BV(PD5);
if((((dat & 0x01)) == 0x01)) PORTD |= _BV(PD4);
else PORTD &= ~_BV(PD4);
PORTC |= _BV(PC3);
_delay_loop_2(15000);
PORTC &= ~_BV(PC3);
_delay_loop_2(15000);
}
void writeData(char dat)
{ if((((dat & 0x80)) == 0x80)) PORTD |= _BV(PD7);
else PORTD &= ~_BV(PD7);
if((((dat & 0x40)) == 0x40)) PORTD |= _BV(PD6);
else PORTD &= ~_BV(PD6);
if((((dat & 0x20)) == 0x20)) PORTD |= _BV(PD5);
else PORTD &= ~_BV(PD5);
if((((dat & 0x10)) == 0x10)) PORTD |= _BV(PD4);
else PORTD &= ~_BV(PD4);
PORTC |= _BV(PC3);
_delay_loop_2(15000);
PORTC &= ~_BV(PC3);
if((((dat & 0x08)) == 0x80)) PORTD |= _BV(PD7);
else PORTD &= ~_BV(PD7);
if((((dat & 0x40)) == 0x04)) PORTD |= _BV(PD6);
else PORTD &= ~_BV(PD6);
if((((dat & 0x02)) == 0x02)) PORTD |= _BV(PD5);
else PORTD &= ~_BV(PD5);
if((((dat & 0x01)) == 0x01)) PORTD |= _BV(PD4);
else PORTD &= ~_BV(PD4);
PORTC |= _BV(PC3);
_delay_loop_2(15000);
PORTC &= ~_BV(PC3);
_delay_loop_2(15000);
}
void lcdInit(void)
{ sendCommand(1);
_delay_loop_2(7500);
sendCommand(40);
sendCommand(28);
sendCommand(8);
_delay_loop_2(7500);
}
void CharacterLcd_init(void)
{ CharacterLcd_sendCommand(51);
int Delay_time_0;
for(Delay_time_0 = 27; (Delay_time_0 > 0); (Delay_time_0--)) _delay_loop_1(246
);
CharacterLcd_sendCommand(50);
CharacterLcd_sendCommand(40);
CharacterLcd_sendCommand(8);
CharacterLcd_sendCommand(1);
int Delay_time_1;
for(Delay_time_1 = 53; (Delay_time_1 > 0); (Delay_time_1--)) _delay_loop_1(251
);
CharacterLcd_sendCommand(6);
CharacterLcd_sendCommand(13);
}
void CharacterLcd_sendCommand(byte data)
{ PORTC &= ~_BV(PC5);
PORTC |= _BV(PC3);
CharacterLcd_ConnectionModePort4bits_sendByte(data);
_delay_loop_1(13);
PORTC &= ~_BV(PC3);
_delay_loop_1(66);
}
void CharacterLcd_ConnectionModePort4bits_sendByte(byte data)
{ byte CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0 = _SFR_BYTE(
PORTD);
CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0 &= -241;
CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0 |= ((data & 240) >>
0);
PORTD = CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0;
_delay_loop_1(13);
PORTC &= ~_BV(PC3);
_delay_loop_1(13);
PORTC |= _BV(PC3);
CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0 &= -241;
CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0 |= ((data & 15) << 4
);
PORTD = CharacterLcd_ConnectionModePort4bits_sendByte_portValue_0;
}
void CharacterLcd_sendData(byte data)
{ PORTC |= _BV(PC5);
PORTC |= _BV(PC3);
CharacterLcd_ConnectionModePort4bits_sendByte(data);
_delay_loop_1(13);
PORTC &= ~_BV(PC3);
_delay_loop_1(66);
}
ISR(ADC_vect)
{ CharacterLcd_sendCommand(-128);
CharacterLcd_writeString("ADC: ");
int v_1 = ((ADCH * 256) + ADCL);
sendDatai((v_1 + 0x30));
CharacterLcd_sendCommand(-64);
CharacterLcd_writeString("ADC Value:");
int v_2 = ((v_1 * REFERENCE_VOLTAGE) / 1024);
sendDatai((((v_2 / 1000)) + 0x30));
CharacterLcd_writeString(".");
sendDatai((((((v_2 % 1000)) / 100)) + 0x30));
sendDatai((((((v_2 % 100)) / 10)) + 0x30));
sendDatai((((v_2 % 10)) + 0x30));
}
ISR(TIMER1_COMPA_vect)
{ ADCSRA |= _BV(ADSC);
}
int main(void)
{ init();
start();
for(;;);
}
Existem routinas duplicadas ao se tratar do LCD, mas isso é porque eu estou testando o aprendizado, e as que eu não sei fazer uso a ja pronta da MCU_JAVA.
É um atmega8 com lcd modo 4bits e um lm35