02-19-12
One of the things I've learned today (and a little bit yesterday) is that it is remarkably difficult to get even simple example programs working on these small chips. Perhaps I had been looking in the wrong places with the wrong search terms.
After trying a great number of versions of different code that would get the chip to accept a switch as an input, I finally stumbled across
this one, in the "reading an input" section. It's written for a different environment with a different programmer but the code example I tried worked the first time without a hitch.
I spent a decent chunk of time staring at the
datasheet, in particular, section 10.2. I was told by Alex that it's essential to pull up the index side-bar for your PDF viewer, otherwise the 234 page document is completely unmanageable. I like how well everything is organized.
from 10.2, here are some of the main points I gleaned:
- DDRB sets the pins to either inputs or outputs.
- 1 sets the corresponding PINxn pin as an output
- 0 sets the corresponding PINxn pin as an input
- PORTB sets the corresponding the port pin (PB0-PB5) to be driven high or low.
- 1 sets the pin to high
- 0 sets the pin to low
- PINB shows the current state of the input and output pins.
- 1 means the pin is currently driven high
- 0 means the pin is currently driven low
I think it was the combination of not knowing exactly which resistors to use and imprecise google search terms that lead to the extreme difficulty I've experienced in getting this simple example to work.
I think my next step will be to get analog inputs and outputs working. From there,
in class
Aaron asked about how to spread out C code between several files.
Here's an example. Three files: demo.c, jims_lib.c, jims_lib.h
/* demo.c */
#include <stdio.h>
#include "jims_lib.h"
int main(){
int y = 7;
printf(" y=%i, f(y)=%i \n", y, double_it_add_one(y));
}
/* jims_lib.c */
#include "jims_lib.h"
int double_it_add_one(int x){
return ONE + 2*x;
}
/* jims_lib.h */
#define ONE 1
int double_it_add_one(int x);
Then to compile and run 'em:
$ cc jims_lib.c demo.c -o demo # compile both of 'em
$ ./demo # run it.
Jim says:
Please add C code for this week, and circuit diagram.