Hi! I am new to microcontroller programming so please be patient. the following will seem like a stupid question.
I am using DEMO9S08AW60E and using Codewarrior(Version: 5.9) with PE.
-------------Code Begins.
PTFDD = 0x00; // to activate/intialize the port F and D
PTDDD = 0x00;
if ((PTDD_PTDD2 |= 0x02))
{
PTFD_PTFD0 |=0x01;
}
else
{
PTFD_PTFD0 &=0x01;
}
-------------Code Ends.
link for DEMO9S08AW60E: http://cache.freescale.com/files/microcontrollers/hardware_tools/schematics/DEMO9S08AW60_SCH.pdf
According to the schematic of the board, Switch 4 is connected to PortD/Pin2. And the set of LED are connected to the PortF/Pin(0-7). I have used a pull up resistor with the help of PE. The idea is to switch on the Led when the swtich is pressed. Logically i feel my code is fine but there doesnt seem to be any effect. I would like to know where i am going wrong and hope you will able to help.
And also it will be great if someone could tell me how to add delay in a loop for example when i am using if else loop.
THANKS in advance!
Solved! Go to Solution.
How to enable pull resistors in HCS08 is pretty much undocumented in the manual. What you have to do is this:
- Write to PTnPS to enable interrupts. Yes, it doesn't make sense. But it will enable the next register, PTnES.
- Write to PTnES to select interrupt edge trigger. Yes, it doesn't make sense. But it will at the same time set the pull resistor polarity: pull-up or pull-down.
- Write to PTnPE to enable the pull resistor.
- Write zero to PTnDD to set the pin as input.
---
That being said, your code has the following problems:
- You aren't using any contact de-bouncing. You need some form of simple contact de-bouncing algorithm, or your program will read random inputs from the switch.
- You are using the bitwise operators incorrectly. To check if a port input is set, you should have written PTDD_PTDD2 & 0x02, meaning "if the port AND the value 0x02 gives me 0x02 rather than 0x00 as result, then...". Similarly you are setting the output incorrectly, it should have been PTFD_PTFD0 |= 0x01; // set to pin to 1, or PTFD_PTFD0 &=~0x01; // set pin to 0.
Also, you seem to be using Freescale's default register map with non-standard bit-fields. Since that crappy register map isn't written in standard C, nobody knows what will happen when you write 0x02 to a single bit, the C standard doesn't document or specify it. I would personally stick to C instead of using Freescale non-standard features.
How to enable pull resistors in HCS08 is pretty much undocumented in the manual. What you have to do is this:
- Write to PTnPS to enable interrupts. Yes, it doesn't make sense. But it will enable the next register, PTnES.
- Write to PTnES to select interrupt edge trigger. Yes, it doesn't make sense. But it will at the same time set the pull resistor polarity: pull-up or pull-down.
- Write to PTnPE to enable the pull resistor.
- Write zero to PTnDD to set the pin as input.
---
That being said, your code has the following problems:
- You aren't using any contact de-bouncing. You need some form of simple contact de-bouncing algorithm, or your program will read random inputs from the switch.
- You are using the bitwise operators incorrectly. To check if a port input is set, you should have written PTDD_PTDD2 & 0x02, meaning "if the port AND the value 0x02 gives me 0x02 rather than 0x00 as result, then...". Similarly you are setting the output incorrectly, it should have been PTFD_PTFD0 |= 0x01; // set to pin to 1, or PTFD_PTFD0 &=~0x01; // set pin to 0.
Also, you seem to be using Freescale's default register map with non-standard bit-fields. Since that crappy register map isn't written in standard C, nobody knows what will happen when you write 0x02 to a single bit, the C standard doesn't document or specify it. I would personally stick to C instead of using Freescale non-standard features.
THANKS A LOT, DON WOOD. That was truly the help i needed.
Just one question, SO my Logic is wrong? i should always use Masks to access the ports, right??
Also, would you explain a little more about the Last paragraph of your's; specially what do you mean by " ... I would personally stick to C instead of using Freescale non-standard features."
Thanks again!!
Yes your logic is wrong, you need to study how to correctly use the bitwise operators in C.
Regarding standard C, Freescale provides a default register map which is implemented using non-standard bit fields. Your code is trying to write more than 1 bit to a single bit location in their bit-field. What happens when you do that, nobody can tell, since Freescale's register maps aren't standard.
Personally I generate my own register maps using a little parser program I've written, which goes through the MCU manual pdf and fetches every register name and bit mask from there, then generates code which is 100% conforming with the C standard.
I think it is sad that Freescale can't deliver libraries that are compliant with standard C and MISRA-C. Instead they focus on providing buggy Eclipse IDEs that nobody asked for. Apparently their new target customers are hobbyist PC programmers, rather than professional car manufacturers.
i have learned about my mistake. thank you for tat.
The PortD needs to be initilised to output ports: PTDDD bits should be set to 'ones'
Thank you DON Wood, i realised my mistake and corrected but the problem still remains.