//duty cycle will be 63%
#define BIT6_MASK 0x40 // 0100 0000 --> Pin 6
#define PORTD_MASK 0x2B //for output port
#define DDRD_MASK 0x2A //for the DDRD
#define PINB_MASK 0x23
#define ADC_PIN 0
unsigned char *SREG_ESP;
unsigned char *portDDRD; //declaring DDRD
unsigned char *portPinB;
unsigned char *portD; //declaing port D to use as output D
// Register Pointer Declaration
unsigned char *TCCR0A_ESP; //0x80
unsigned char *TCCR0B_ESP; //0x81
unsigned char *TCNT0_ESP; //0x85
unsigned char *OCR0A_ESP; //0x89
unsigned char *OCR0B_ESP; //0x89
unsigned char *TIMSK0_ESP; //0x6F
unsigned char *ADMUX_ESP;
unsigned char *ADCSRA_ESP;
unsigned char *ADCSRB_ESP;
unsigned char *ADCL_ESP;
unsigned char *ADCH_ESP;
void setup(){
ADMUX_ESP = (unsigned char*) 0x7c;
ADCSRA_ESP = (unsigned char*) 0x7a;
ADCSRB_ESP = (unsigned char*) 0x7b;
ADCL_ESP = (unsigned char*) 0x78;
ADCH_ESP = (unsigned char*) 0x79;
*ADCSRA_ESP |= 0b10000000;
//////////////////////////////////////////////////
// Serial Library
Serial.begin(9600); // open the serial port at 9600 bps:
//////////////////////////////////////////////////
// SREG
SREG_ESP = (unsigned char *) 0x5F;
// Assigned Register Address
TCCR0A_ESP = (unsigned char *) 0x44;
TCCR0B_ESP = (unsigned char *) 0x45;
TCNT0_ESP = (unsigned char *) 0x46;
OCR0A_ESP = (unsigned char *) 0x47;
OCR0B_ESP = (unsigned char *) 0x48;
TIMSK0_ESP = (unsigned char *) 0x6E;
*TCCR0A_ESP = 0b10000011; // 0X82
*TCCR0B_ESP = 0b00000101; // 0X02
*OCR0A_ESP = 160; //duty cycle
/* Enable OC0 as output. Pd6*/
portDDRD = (unsigned char *) DDRD_MASK;
*portDDRD |= BIT6_MASK; // Configure PD bit 6 as an output
portD = (unsigned char *) PORTD_MASK;
/* Enable timer 0 overflow interrupt. (TOIE0)*/
*TIMSK0_ESP = 0x01; // enable timer compare interrupt - BIT1
// Global Interrupt Enabled
*SREG_ESP = 0x80;
}
ISR (TIMER0_OVF_vect)
{
}
int main (void)
{
setup ();
/* loop forever, the interrupts are doing the rest */
for (;;) {
Serial.print(adc_read(ADC_PIN)); // print as an ASCII-encoded decimal - same as "DEC"
Serial.print("\n");
}
return (0);
}
uint16_t adc_read(uint8_t adcx) {
/* adcx is the analog pin we want to use. ADMUX's first few bits are
the binary representations of the numbers of the pins so we can
just 'OR' the pin's number with ADMUX to select that pin.
We first zero the four bits by setting ADMUX equal to its higher
four bits. */
*ADMUX_ESP &= 0xf0;
*ADMUX_ESP |= adcx;
/* This starts the conversion. */
*ADCSRA_ESP |= 0b01000000;
/* Finally, we return the converted value to the calling function. */
unsigned char cl = *ADCL_ESP;
unsigned char ch = *ADCH_ESP;
return (cl | (ch << 8));
}