1
votes

Here's what I have to dp:

Consider an ATmega324A development board and a CSSE2010/CSSE7201 IO Board. Switches S3 to S0 are connected to AVR port B pins 3 to 0. Push button B0 is connected to AVR port A pin 0. LEDs L0 and L2 and connected to AVR port C pins 0 and 2 respectively. LED L0 (red) is the “Locked” LED and should only be on when the lock is locked. LED L2 (green) is the “Unlocked” LED and should only be on when the lock is unlocked. The lock initially starts in the locked state. The user enters the binary code for a digit on the switches (S3 to S0) and then presses and releases push button B0 to “enter” the first digit. The user then enters the binary code for the second digit on the switches and presses and releases push button B0 to “enter” the second digit. If the digits match the expected value (the last digit of your student number followed by the third digit of your student number) then the lock should be “unlocked”, otherwise it should stay in the locked state until the two digits are entered correctly.

Here is my code so far:

#include <avr/io.h>

/* Seven segment display values */

uint8_t seven_seg[16] = { 63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113};
int main(void) {
uint8_t digit;
uint8_t temp;
uint8_t digit2;
uint8_t code[2] = {6,3}


DDRA = 11111110;   //port A is input (last bit)
DDRB = 0X00;       //port B is input
DDRC = 0x0F;   //port c is output
DDRD = 0XFF;   //set port D to be output

while(1) {
    /* Read in a digit from lower half of port C pins */
    /* We read the whole byte and mask out upper bits */

    PORTB = 1;  //Led is red
    clock = PINA & 00000001;   //read in last bit of port A
    temp = PINB & 0X0F;  //read in lower half of port b


    /* Checks to see the first digit is correct */
    if(temp == code[0] && clock == 1) {
        digit = temp;           
        PORTD = seven_seg[temp];

        } else {
        PORTD = 0;
    }



}

I'm getting stuck at the point where I have to read in the second digit. Would I be doing this inside a nested loop of the first? Or how would I go about reading in two digits from my switches, which is clocked in each time from the press of a button?

2
This question appears to be off-topic because it is about embedded programming and should be moved to Electrical Engineering StackExchangeDon't You Worry Child
@Don'tYouWorryChild As long as it is mainly about programming and not hardware, it isn't off-topic for SO. However, there is a higher chance of getting more and better answers at the electronics site.Lundin
@Lundin : Well, Not discouraging these questions will attract more and more embedded problems here, but as you said electronics site is better home for them, so they are better suited there.Don't You Worry Child
Embedded programming questions are encouraged here. And no, the EE site is very narrow in what they consider a fit.Chris Stratton
When you are using binary numbers in your C code you have to add "0bXXXXXX". Else it will handle the number as decimal which is not wanted.MrSykkox

2 Answers

1
votes

To make a variable program that can be used for longer number sequences, simply use a loop. for(uint8_t i=0; i<NUMBER_OF_DIGITS; i++). The port reading needs to be inside the loop.

However, you cannot read buttons the way you do. All buttons have an electro-mechanical signal bounce, which you need to filter out to prevent false readings. You must do this on any kind of embedded system.

The simplest way to do this is to sample the button once, save the result, wait a few milliseconds, then sample it again. If the samples compare equal, accept it as result (pressed or not pressed).

Alternatively, you can trigger an interrupt on the edge of the button signal, from there start a timer, and then when the timer runs out, read the port.

More advanced methods use some form of median filters.

0
votes

So if i get you right you wanna do something like this

Start:
Wait for pushbutton
    Wait for first digit
Wait for pushbutton
    Wait for second digit
If digits they are the same
    Turn off Red LED
    Turn  on Green LED
Else
    Goto start   

Can you confirm this? It is always a good thing to do your code in pseudo first. It gives very good overview of what you want to do.