I have been trying to get confident with input/output code writing, but have a problem.
Here is a code snippet that works, modified from LV168Demo1.c
#include <pololu/3pi.h>
#include </usr/avr/include/avr/io.h>
void initialize()
{
pololu_3pi_init(2000);
}
int main()
{
initialize();
DDRD &= ~(1 << PD0);
PORTD |= (1 << PD0);
while(1)
{
if(PIND & (1 << PD0)) {
left_led(0);
set_motors(50,50);
}
else
left_led(1);
set_motors(0,0);
}
And here is a snippet from the AVR Tutorial which does not work.
http://elecrom.wordpress.com/2008/02/12/avr-tutorial-2-avr-input-output/
#include <mega16.h>
#include <delay.h>
//declare global arrays for two patterns
unsigned char p1[4] = { 0b10000001,
0b01000010,
0b00100100,
0b00011000 };
unsigned char p2[4] = { 0b11111111,
0b01111110,
0b00111100,
0b00011000 };
void main()
{
unsigned char i; //loop counter
DDRB = 0xFF; //PB as output
PORTB= 0x00; //keep all LEDs off
DDRC = 0x00; //PC as input
PORTC.0 = 1; //enable pull ups for
PORTC.1 = 1; //only first two pins
while(1)
{
//# if SW0 is pressed show pattern 1
if(PINC.0==0)
{
for(i=0;i<3;i++)
{
PORTB=p1[i]; //output data
delay_ms(300); //wait for some time
}
PORTB=0; //turn off all LEDs
}
//# if SW1 is pressed show pattern 2
if(PINC.1==0)
{
for(i=0;i<3;i++)
{
PORTB=p2[i]; //output data
delay_ms(300); //wait for some time
}
PORTB=0; //turn off all LEDs
}
}
}
Why can’t I use the above nomenclature in a 3pi environment, like DDRB =0x00, etc ? To me, it seems easier to use than DDRD &= ~(1 << PD0);, which I don’t fully understand.
Maybe a good tutorial on this nomenclature would be very helpful to C newbies like me.
Thanks…donde