Atmel C code understanding

Status
Not open for further replies.

aliyesami

Full Member level 6
Joined
Jan 7, 2010
Messages
369
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
USA
Visit site
Activity points
4,190
this is a working code that is combining the databits on two different ports and I am not understanding how it is working.
if we AND PORTB to ~0x3F which is 0xC0 , it will make bits DB0-DB5 all zeros so how is the PORTB and PORTC
assignments working ?

Code:
/*-------------------------------------------------
DATA mapping:
    DB0    DB1   DB2    DB3   DB4   DB5    DB6   DB7 
    
    PB0    PB1   PB2     PB3   PB4   PB5    PC6   PC7

CONTROL mapping:
    RES  RD   WR  RS
    PC2  PC3  PB4 PB5
    
---------------------------------------------------*/
 #define DMSK				0x3F
 

void write_8(unsigned char c)
{
	PORTB = (PORTB & ~DMSK)|(c & DMSK); 
	PORTC = (PORTC & DMSK)|(c & ~DMSK);
     	.
	.
}

void TFT_setup(void)
{
	DDRB |= DMSK;               
	DDRC |= ~DMSK;			
	DDRC |= 0x3C;              
        .
        .
}
 

At the top you have the pinout guide showing what maps to what. From the look of it is to a LCD. DB0 connects to PORTB.0, DB1 connects to PORTB.1 and so on

Next is a constant declaration. 0x3F hex is for 00111111. This is later on in the code

The first line in write_8 places lower 6 bits of input c on the lower 6 bits of PORTB or DB0-DB5.
The next line places the upper 2 bits of input c on the upper 2 bits of PORTC or DB6-DB7.

The first line in TFT_setup sets the lower 6 bits of DDRB to 1.
The next line sets the upper 2 bits of DDRC to 1.
The next line sets the middle 4 bits of DDRC to 1.

This code used bit wise operations to set and clear the data lines for PORTB and PORTC.
 

so i change the pin mapping to

DATA:
DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

PB0 PB1 PB2 PC3 PC4 PC5 PC6 PC7

CONTROL:
RES RD WR RS
PC2 PB5 PD3 PD2

would the following code works ?

Code:
void write_8(unsigned char c)
{
 	PORTB = (PORTB & 0xF8)|(c & 07);  // Read PORTB and mask to keep upper 5 bits, Mask c to keep lower 3 bits,  OR both together and write back to PORTB
 	PORTC = (PORTC & 1F)|(c & F8);    // Read PORTC and mask to keep lower 3 bits, mask c to keep upper 5 bits,  OR both and write back to PORTC
}

void TFT_setup(void)
{
DDRB |= ((1<<PORTB0)|(1<<PORTB1)|(1<<PORTB2)|(1<<PORTB5)) 
DDRC |= ((1<<PORTC2)|(1<<PORTC3)|(1<<PORTC4)|(1<<PORTC5)|(1<<PORTC6)|(1<<PORTC7))
DDRD |= ((1<<PORTD2)|(1<<PORTD3))
}

please note I am using one DDRC command to set the data and control bits as output and not two DDRC commands as in original post.
 
Last edited:

Code:
PORTC = (PORTC & 1F)|(c & F8);    // Read PORTC and mask to keep lower 3 bits, mask c to keep upper 5 bits,  OR both and write back to PORTC
This should be:
PORTC = (PORTC & 0x07) | (c & 0xF8);

Code:
DDRB |= ((1<<PORTB0)|(1<<PORTB1)|(1<<PORTB2)|(1<<PORTB5)) 
DDRC |= ((1<<PORTC2)|(1<<PORTC3)|(1<<PORTC4)|(1<<PORTC5)|(1<<PORTC6)|(1<<PORTC7))
DDRD |= ((1<<PORTD2)|(1<<PORTD3))
This should be:
DDRB |= 0x27;
DDRC |= 0xFC;
DDRD |= 0x0C;
 

Code:
PORTC = (PORTC & 1F)|(c & F8);    // Read PORTC and mask to keep lower 3 bits, mask c to keep upper 5 bits,  OR both and write back to PORTC
This should be:
PORTC = (PORTC & 0x07) | (c & 0xF8);

oops yes i see my mistake .. thanks

I am not understanding why? what am I doing wrong in setting all the output pins direction to 1 ?

thanks
 


I think the macros are PB0 style instead of PORTB0 style. I could be wrong. Either way you could just manually encode them in hex. Which is shorter.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…