NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Math Problem

January 15, 2011
by Jalex
Jalex's Avatar

Hi I didn't want to use a timer as the RC time constant is very short. 40 pf and 1.5 meg ohm I have a separate circuit board with a comparator that goes high when the input drops below .7 volts. An RC is connected to it's input. I am trying to get it to measure degrees of a trimmer cap. As you can see I added it to your code just to play around but I have some errors. It uses 2 input pins and 1 output pin to monitor the comparator. The third pin needs to trigger the math that adds the difference to the midway point. // initialload.c // for NerdKits with ATmega168 // mrobbins@mit.edu

define F_CPU 14745600

include <stdio.h>

include <avr/io.h>

include <avr/interrupt.h>

include <avr/pgmspace.h>

include <inttypes.h>

include "../libnerdkits/delay.h"

include "../libnerdkits/lcd.h"

// PIN DEFINITIONS: // // PC4 -- LED anode

int main() { // LED as output DDRC |= (1<<PC4); // Make Pin5 pin6 an input DDRC &= ~(1<<PC5); DDRC &= ~(1<<PC6); // turn on LED PORTC |= (1<<PC4); // int Result as 16 bit uint16_t Result = 0; // fire up the LCD lcd_init(); lcd_home();

// print message to screen // 20 columns wide: // 01234567890123456789 lcd_line_one(); lcd_write_string(PSTR(" Jim Alexander's ")); lcd_line_two(); lcd_write_string(PSTR("****")); lcd_line_three(); lcd_write_string(PSTR(" First Program ")); lcd_line_four(); lcd_write_string(PSTR(" is alive and well ")); // I am trying to just add 3 digit integer to the end of the forth line.


//fprintf_P(&lcd_stream, PSTR(is alive and well: %.3f"), Result);


// turn off that LED PORTC &= ~(1<<PC4);

// busy loop while(1) {

// LED as output
DDRC |= (1<<PC4);

// turn on LED
PORTC |= (1<<PC4);

//delay for 500 milliseconds to let the light stay on
delay_ms(500);

// turn off LED
PORTC &= ~(1<<PC4);

// delay for 500 milliseconds to let the light stay off
delay_ms(500);
// reset Result for new count
Result = 0;
    // Turn on PC4 
PORTC |= (1<<PC4);
delay_ms(5);
//turn PC4 back off
PORTC &= ~(1<<PC4);
// Count to 180 only when PC5 is low
while (Result < 180) && (PINC & (1<<PC5)) {
 Result ++; }

// error is expected ";" before "(" token


// If IR is on then add the difference to 180
if (PINC & (1<<PC6) = 1) {
 Result = (180 - Result) + 180;

}

}

return 0; }

Post a Reply

Please log in to post a reply.

Did you know that an electroluminescent backlight for an LCD panel requires hundreds of volts AC to run? Learn more...