Ads

Showing posts with label alarm_clock. Show all posts
Showing posts with label alarm_clock. Show all posts

Saturday, November 15, 2008

Adding a shift register to the mix

So I hooked up a shift register (SN74HC595N) (8 bits output) to the ATMega168 (avr).

I wrote code. It wouldn't work. I couldn't figure out why. It sometimes worked, sometimes didn't. The lower 4 bits would work, sometimes...

Turns out, I forgot to set the 'you allowed to send information/output stuff' (SRCLR (overriding clear)) pin to high (tie it to vcc), so it was floating. This made it work sometimes, and most of the time it didn't work.

Once I put +5v on pin 10 (SRCLR) everything worked out great. I cleaned up all of the crazy code that I wrote trying to get something to work (I thought it was a code problem, not a "I missed hooking up a pin"). I had LED's hooked up to the 8 outputs of the shift register. It just lights the last led, then the next up(with the first), and etc until it lights all of them.


oh, for the connecting of stuffs, I used this diagram


from http://www.windmeadow.com/node/20 (note: On that page, he mentions adding power to the shift register as its not on the diagram. I didn't read that until making this post, but just remembered to do it from the data sheet on the shift register. Sucks that I didn't remember to hookup pin 10!) I put the LEDS on QA -QH.

the code (blinkin.c):

#include <avr/io.h>
#include <util/delay.h>

#define nop() __asm__ __volatile__ ("nop")
#define F_CPU 20000000UL //My chip is 20 Mhz
//make sure F_CPU is after #include <util/delay.h>

void
send_byte_to_shift_register(int8_t bite);

int
main(void)
{

int
j = 1;

while
(1)
{

send_byte_to_shift_register(j-1);
_delay_ms(500.0);

j = j*2;
if
( j == 512)
{
j = 1;}
}



return
0;
}


void
send_byte_to_shift_register(int8_t bite)
//should be able to use SPI instead of this way...
{

DDRB = 0xFF;

int
i = 0;

int
out = 0;

for
(i=0;i<8;i++)
{


out = (bite >> i) & 0x01; //extract 0 or 1
//0x01 for last bit only, and >> to get location i
if (out == 1) {PORTB = (1 << PINB3); }
else
{PORTB = (1 >> PINB3); }
PORTB = (1 << PINB1); //clock;
}
PORTB = (1<<PINB2); //latch the shift register

return
;
}


yeah, so my sister yells out "STELLLLA" at the end of the video. Think of it as a bonus.

Tuesday, October 28, 2008

Easier than I thought...

So I had a free 15 minutes today and the result:



I implemented the four LED counter that I was talking about last time. One of the pins that I used is also used to program the device, so I have to move the reset and that pin (17/PB3/MOSI).

code:
#include < avr/io.h >
#include < util/delay.h >

#define nop() __asm__ __volatile__ ("nop")
//My chip is 20 Mhz
#define F_CPU 20000000UL

int main(void)
{
DDRB = 0xFF;
PORTB = 0x0;
while(1)
{
PORTB += 1;
_delay_ms(1000.0);

if(PORTB == 16) {PORTB = 0;}
}
return 0;
}

//endcode
So, I just realized that my header info last time probably didn't work, as I used < and > .

Next step, do the same thing with the shift register chip in there.
After that, figure out timers/interrupts.

Friday, October 24, 2008

Great success!



I was having lunch with a good friend on Wed and we mentioned some interesting projects that we could get into. I stopped myself from dreaming because I have not worked on the alarm clock for a long time. I resolved to write code, flash it, and blink an led by the end of the week.

As you can see above, I made the LED blink. Next step is to setup 4 or 8 LEDs and make them "count" from 0000->1111 (0->16). Then do the same, but using a shift register.

The reason that I have not done it before is because I was scared. It really was as easy as reading 1-2 pages in the datasheet. I had to figure out how to make a "nop" (no operation) but I could have just made the device add a number or something. There seems to be delay code already written in util/delay.h so I will try and use that in the 0->16 counter step to make it change once a second. I also turned all of the ports in "B" to output and on, so I should be able to just add LEDs and have 8 that flash together.

Summary:
xn ... so x can be like, C or B and n can be lik 0-7 (makes sense if you see the pin names)
DDxn: 1->output , 0->input
DDxn configures how the PORTxn acts.
PORTxn: input: 1->pullup resistor active,0->pull up resistor inactive
ouput: 1->driven high,0->driven low
PINxn: 1 -> toggle PORTxn
There was some warning about switching between input and output, it works better if you make sure that you use a specific sequence if you don't want the pin to be in a random state for a period of time.

My LED was on Pin 14 on the DIP, otherwise known as PORTB0 (PB0).

Code (blinkin.c):
#include < avr/io.h >
#define nop() __asm__ __volatile__ ("nop")

int main(void)
{
int i = 0;

DDRB = 0xFF; // set all B's to output
while(1)
{
PORTB = 0xFF; //All On
for(i=0;i<30000;i++)
{ nop(); }

PORTB = 0x00; //All off
for(i=0;i<30000;i++)
{ nop(); }
}

return 0;
}

for the record, these are the commands used to compile/flash:
avr-gcc -I. -I/usr/local/AVR/avr/include -g -mmcu=atmega168 -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -Wa,-ahlms=blinkin.lst -c blinkin.c -o blinkin.o

avr-gcc -Wl,-Map,blinkin.out.map -mmcu=atmega168 -lm -o blinkin.out blinkin.o

avr-objcopy -j .text -j .data -O ihex blinkin.out blinkin.hex

avr-objcopy -j .eeprom \

--change-section-lma .eeprom=0 \
-O ihex blinkin.out blinkin.ee.hex

avrdude -c bsd \
-p m168 -P /dev/parport0 -e \
-U flash:w:blinkin.hex


I didn't write those all by hand, I found a nifty Makefile online that I configured, all I have to do is type "make writeflash" to compile/flash it onto the device. I can post the Makefile if anyone is interested.

The big problem that frustrated me was that I wasn't exactly sure how to "start" the code. So for a while I thought I did it wrong because I couldn't even make the LED light. I originally just kept cycling the power. That does not work if you have the programmer still plugged in. Turns out that if you pull the reset line out (pin 1), you start the device.You can leave the other programming pins attached. I might wire a switch to that line so that I can leave the programmer attached and run the code without pulling out a wire and reinserting it.

Should be relatively smooth sailing from here, I am assuming that the hardest parts are setting up the compiler/flasher, learning how to make the code run, and plugging everything into the correct pins.

Tuesday, August 05, 2008

Power supply for Alarm Clock

I made a power supply for the chip last night. I used a wall wart (transformer) from an extra hub that I had lying around. It was 5V which was what I was looking for. I initially de-soldered the connector from the hub, and soldered wires to the pins. This way I wouldn't have to modify the actual plug from the wall wart.

Fate decided that I was being foolish. I spent about an hour trying to figure out how I could have continuity between two points, but those two points would not carry a voltage. It turns out that it works when they were not inserted into each other, but once you plug it in, the connection would be broken on the "sleeve" part, not the pin part.

So, plan B. Just stick a wire in the hole on the connector and another wrapped around the outside, held with duct tape and hope.

I also soldered little jumper wires to the wires at the end of the programmer that I made last time. This way I can stick it into the breadboard without breaking the wire or crying.

Next post will be about sending my first program to the ATMega168, which now has power, and a programmer. I don't think I need anything else just yet, I choose that chip because it didn't need any support circuitry.

Sorry if the descriptions are confusing, a picture or diagram would help, but I didn't get my memory card for my phone yet.

Saturday, July 19, 2008

Alarm Clock First Post

I finally started doing design/testing for the alarm clock idea that I came up with about Christmas last year.

I built the programmer for the AVR microcontroller that I am using. Its the simplest programmer and uses the parallel port. Its basically a parallel port housing with 1k resistors in series to specific pins. That is it. Fits in the housing, so a picture would be pretty boring. I have to remember to apply power to the chip separately.

Which points to my next mini-step, making a stable power supply. I considered just using the 5v lines from my motherboard, but I will probably use a spare power supply that I have. I hooked up a usb hub to the motherboard power, and every once in a while I manage to short it and my computer shuts down. That would be bad in development and I don't want any fires/broken computer.

Pinouts follow:


ColorAVRParallel PortName
Blackpin 1pin 7AVR/RESET
Greenpin 19pin 8AVR SCK (clock input)
Redpin 17pin 9AVR MOSI (instruction in)
Whitepin 18pin 10AVR MISO (data out)
Silverpin 22/8pin 18Signal Ground

I need to check the pinouts on my specific chip, I'll update this page with the chip model number/checked pinouts when I get there.

Reference: AVRDUDE
Its dirt cheap and really basic. Now to find out if it works... LED blinking, here I come!