prog will not past the "for lus

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

i want to shift out bits on a output pin (no uart, i2c or anything like that)

i made a prog based on a example from this forum, this prog will provide a clockpuls as wel.

so i modified the prog for my aplication.

first is the example from this forum, second is mine.

for (mask=0x80000000; mask !=0; mask>>=1)
		{
			PORTA &= ~clk_mask;
			if (long_value & mask)
			{
				PORTA |= data_mask;
			}
			else
			{
				PORTA &= ~(data_mask);
			}

			PORTA |= clk_mask;
		}

 

 

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

int mask;

int main(void)
{    
    DDRA = 0b11111111;
    DDRC = 0b11111111;
    
    TCCR1B |= (1<<CS10);  // prescaler = 1024    | (1<<CS12)
    
    while (1) 
    {
        if(TCNT1>2)
        {
            PORTC ^= 1<<PINC1; //clock toggle
            PORTA ^= 1<<PINA0; //toggle LED for check
            TCNT1=0; //reset counter
            
            
            for (mask=0x80000000; mask !=0; mask>>=1) // based on 32 bits
            {
                PORTA |= (1 << PINA1); // made A1 high fo checking
                if (0x80808080 & mask)    // the 32 bits "anden" with the mask
                {
                    PORTC |= (1 << PINC0); // bit shiftout = 1
                }
                else
                {
                    PORTC &= ~(1 << PINC0); // bit shiftout = 0
                }
                
                PORTC |= (1 << PINC2); // made latch high
                _delay_ms(100); // time that latch is high
                PORTC &= ~(1 << PINC2);    // made latch low
            
            }
        }        
    }
}
 
Last Edited: Sat. Jun 1, 2019 - 09:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

trixo wrote:

int mask;

int will be 16 bits on these AVRs.

If you try and assign 0x80000000 to a 16 bit variable you won't get what you want!

You need a 32 bit type (and unsigned would make more sense here)

Use :-

unsigned long mask;

Or use the stdint types :-

#include <stdint.h>

uint32_t mask;

 

 

 

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

good point, yes

i think i find out this problem when i passed the for loop by my self. smiley

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

Guessing it's an AVR, nothing to do with Studio, moving to AVR forum.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly