ATmega 16 need help fast !

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi , i'm working on a midi controller and i need to finalize it t'ill tomorrow . Everything works fine except one switch on PINB7 please help! The weirdest part is that it gives power and the switch led (built in) turn on and off but without any midi signal.

Here's the code for the buttons:
 

// Buttons on PORTB1...B5 (inputs and internal pull-up)
    DDRB &= ~(1<<DDB0);    PORTB |= (1<<PB0);
    DDRB &= ~(1<<DDB1);    PORTB |= (1<<PB1);
    DDRB &= ~(1<<DDB2);    PORTB |= (1<<PB2);    
    DDRB &= ~(1<<DDB3);    PORTB |= (1<<PB3);
    DDRB &= ~(1<<DDB4);    PORTB |= (1<<PB4);
    DDRB &= ~(1<<DDB5);    PORTB |= (1<<PB5);
    DDRB &= ~(1<<DDB6);    PORTB |= (1<<PB6);
    DDRB &= ~(1<<DDB7);    PORTB |= (1<<PB7);
    
}

// Function gets the key that changed as binary (e.g.: 0b00000010 means,
// that the key on PB1 changed).
static uchar keyPressed(uchar key)
{
    uchar i, mask;
    mask = 1;
    
    // compare key to mask
    for (i = 0; i <= 7; i++) 
    {
        if ((~key & mask) == 0)
            return i;
        mask <<= 1;
    }
    return 0;
}

int main(void)
{
    // variable declaration
    // ====================
    uchar key = 0;                                // number of key, that changed 
    uchar keyState = 0;                            // state of the key that actually changed (0 or 1)
    uchar keyMask = 0;                    // used to save old state of PORTB0...7
    uchar pinMask = 0;                    // used to compare the current state of PORTB to the previous state
    uchar keyDidChange = 0;                        // variable for the if 
    int adcOld[8] = { -1, -1, -1, -1, -1, -1, -1, -1, };        // array for old ADC values (used to compare current state)
    uchar channel = 0;                            // ADC channel, that has actually changed
    int value;                                    // value of the ADC that actually changed (8-bit precision)
    uchar midiMsg[8];
    uint16_t control_value;                            // array for the control change midi message
    wdt_enable(WDTO_1S);
    hardwareInit();
    odDebugInit();
    usbInit();
    // enable interrupts
    sei();
    
    // Main Event Loop
    // ===============
    while(1)
    {            
        wdt_reset();
        usbPoll();
        
        // read state of the keys (at PORTB0...7)
        pinMask = 0 | PINB;
        
        // compare PINB with previous state
        if (keyMask != pinMask)
            keyDidChange = 1;
        if (usbInterruptIsReady()) 
        {
            // Keys:
            // ========
            if (keyDidChange) 
            {
                key = keyPressed(keyMask ^ pinMask);
                // key debouncing
                _delay_ms(20);
                
                // For description of USB MIDI msg see:
                // http://www.usb.org/developers/devclass_docs/midi10.pdf
                    
                // Key pressed: keyState = 0,
                // Key released: keyState = 1.
                keyState = ((pinMask >> key) & 1);
                                        
                // MIDI Control Change message
                midiMsg[0] = 0x0b;
                midiMsg[1] = 0xb0;
                midiMsg[2] = key + 13;            // MIDI note 14...21
                midiMsg[3] = keyState * 127;    // 0 or 127
                usbSetInterrupt(midiMsg, 4);
                
                keyDidChange = 0;
                keyMask = pinMask;    
            } 

 

This topic has a solution.

Last Edited: Sat. May 23, 2015 - 03:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

What is connected to PB7?  What is the PB7 voltage level RIGHT ON THE PIN when the switch is made?  What is the PB7 voltage level RIGHT ON THE PIN when the switch is not made?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

there's no physical errors i just somehow after 6 hours of tests found out how to make it work in the code. replaced :

 

 DDRB &= ~(1<<DDB0);    PORTB |= (1<<PB0);
    DDRB &= ~(1<<DDB1);    PORTB |= (1<<PB1);
    DDRB &= ~(1<<DDB2);    PORTB |= (1<<PB2);    
    DDRB &= ~(1<<DDB3);    PORTB |= (1<<PB3);
    DDRB &= ~(1<<DDB4);    PORTB |= (1<<PB4);
    DDRB &= ~(1<<DDB5);    PORTB |= (1<<PB5);
    DDRB &= ~(1<<DDB6);    PORTB |= (1<<PB6);
    DDRB &= ~(1<<DDB7);    PORTB |= (1<<PB7);

with:

PORTB = 0xff;
DDRB = 0;	
	

 

thanks for replying but i just found the solution by myself (i still don't know how it worked).

 

P.s  this isn't my code it's from the Phi-T control project so don't forget to check the license in case you using this... (i'm talking about what i posted above not those 2 lines)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
// Buttons on PORTB1...B5 (inputs and internal pull-up) etc.

 

The above does NOT enable internal pull up and the entire bit of code is not necessary as the chip wakes up with all pins in input mode.

 

The following enables internal pull up. That would be the reason it now works.

PORTB = 0xff;

not necessary but OK

DDRB = 0;	

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

zeno wrote:

there's no physical errors i just somehow after 6 hours of tests found out how to make it work in the code. replaced :

 

 DDRB &= ~(1<<DDB0);    PORTB |= (1<<PB0);
    DDRB &= ~(1<<DDB1);    PORTB |= (1<<PB1);
    DDRB &= ~(1<<DDB2);    PORTB |= (1<<PB2);    
    DDRB &= ~(1<<DDB3);    PORTB |= (1<<PB3);
    DDRB &= ~(1<<DDB4);    PORTB |= (1<<PB4);
    DDRB &= ~(1<<DDB5);    PORTB |= (1<<PB5);
    DDRB &= ~(1<<DDB6);    PORTB |= (1<<PB6);
    DDRB &= ~(1<<DDB7);    PORTB |= (1<<PB7);

with:

PORTB = 0xff;
DDRB = 0;	
	

 

thanks for replying but i just found the solution by myself (i still don't know how it worked).

 

Rubbish.   Your long-winded sequence is exactly the same as a simple PORTB=0xFF

If you really get a different result,   I suspect that you have corrupted / edited the system header file.   (i.e. PB7 or DDB7 definition)

 

Look at the generated ASM code in the .LSS file or in the Simulator Disassembly.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No necessary, in the post above yours I had made some silly remarks which were quickly edited out.....blush

 

(minor cleanup achieved. Ross)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Sun. May 24, 2015 - 01:37 AM