pheripheral register access in C Code

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

pheripheral register access in C Code

2,753 次查看
pelikan_tech
Contributor I
Hallo,
 
I'm currently trying to become familiar with the 56800 and codewarrior but one of the simplest things is causing troubles. I was changing the LED_isr example wich is supplied with Codewarrior and tried to access the GPIO_A_DR and also some other pheripheral register directly by writing a value to it. Below you can see the definition I added to MC56F801x.h:
 
#define PORTA   (*(char*) (0x00F101))   // port A
 
..which defines PORTA as GPIO_A_DR
 
the Main function show how I tried to write a value to PORTA which refers to GPIO_A_DR:
 
void main(void)
{
 

init_ports();
init_uart();
while(1)
  {
  PORTA=0x00;                                       //doesn't work
  //asm(bfset #$0001,X:GPIO_A_DR);   //works fine
  delay(); 
  //asm(bfclr #$0001,X:GPIO_A_DR);    //works fine
  delay(); 
  PORTA=0xFF;                                     //doesn't work
  }
}
 
Although I'm sure I set all the other registers concerning GPIO_A the right way (direction register ....) the values asigned to PORTA do not appear at the outputs pins of the 56F8014. Why do  assembler commands work fine while writing to PORTA (which refers to GPIO_A_DR) don't show any effects on the output pins ?
I also tried to write to some registers from the SCI pheripheral but I didn't succeed either...
All other C-Code which does'nt access the pheripheral registers works fine !
There must be a very fundamental problem regarding the compiler settings or something else since I cannot explain myself sucha behaviour....
 
Please help !
 
Regards,
 
Klaus Pelikan
 
标签 (1)
0 项奖励
回复
1 回复

792 次查看
pittbull
Contributor III
Hi Klaus,
I don't know the 56800 but I have some guesses:

Are both, GPIO_A_DR and PORTA at the same address 0x00F101 and have the same width (8 or 16 bits)?

Try it this way:
#define PORTA *(unsigned char volatile *)(0x00F101) // port A
The 'volatile' keyword instructs the compiler to leave out optimizations e.g. using a register instead of the memory address.

You may also look at the compiler output (assembler code) to see what's going wrong.

Good Luck,
pittbull
0 项奖励
回复