![]() |
Forum Index : Microcontroller and PC projects : Help required with C
Author | Message | ||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Can any of you wonderful people help me 'translate' the following MMBasic code into C. The code includes a Sub that simply has an integer passed to it (i.e a byte) which is clocked out one bit at a time on an output pin. Setpin 1, dout Setpin 2, dout pin(1)=1 pin(2)=1 Sub TxOut(TxData) For x = 1 to 8 pin(2) = TxData AND (2^x) 'set data output pin to correct bit value pause 5 'delay a bit pin(1) = 0 ' pulse clock pin low pause 25 pin(1) = 1 Next x End Sub [/code] As Grogster correctly identified, I am a little "C" sick at the moment and hence this new thread. NOTE: I have a delay function in C so don't need any code for that! Appreciate any feedback. ![]() Thanks, Ww |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3272 |
I know that I am going to regret this ![]() You should do the first four commands in BASIC as it is easier to do them there. Then the plugin would be something like this... #define pin1set *(volatile unsigned int *)0xbf886138 = (1 << 9) // pin 1 high
#define pin1clr *(volatile unsigned int *)0xbf886134 = (1 << 9) // pin 1 low #define pin2set *(volatile unsigned int *)0xbf886138 = (1 << 6) // pin 2 high #define pin2clr *(volatile unsigned int *)0xbf886134 = (1 << 6) // pin 2 low void mSec(int dly); // declare all functions void main(unsigned char arg1[]) { int i; char c; c = arg1[1]; // this is the first char in the string for(i = 0; i < 8; i++) { if(c & 1) // if bit zero is a 1 pin2set; else pin2clr; c >>= 1; // shift down the char mSec(5); pin1clr; mSec(25); pin1set; } } // delay for dly * milliseconds // the number 3400 may need to be adjusted // it should be about right for 40MHz void mSec(int dly) { int i; while(dly--) { for(i = 0; i < 3400; i++); } } This needs a string argument where the first character is the byte that you want to send. This is the whole thing in BASIC: PlugIn Data 27BDFFE0 AFBF001C AFBE0018 03A0F021 AFC40020 8FC20020 24420001
PlugIn Data 90420000 A3C20014 AFC00010 0B40082F 00000000 8FC20010 24420001 PlugIn Data AFC20010 8FC20010 28420008 1440FFDA 00000000 93C20014 30420001 PlugIn Data 304200FF 10400007 00000000 3C02BF88 34426138 24030040 AC430000 PlugIn Data 0B40081B 00000000 3C02BF88 34426134 24030040 AC430000 83C20014 PlugIn Data 00021043 A3C20014 24040005 0F400839 00000000 3C02BF88 34426134 PlugIn Data 24030200 AC430000 24040019 0F400839 00000000 3C02BF88 34426138 PlugIn Data 24030200 AC430000 03C0E821 8FBF001C 8FBE0018 27BD0020 03E00008 PlugIn Data 00000000 27BDFFF0 AFBE000C 03A0F021 AFC40010 0B40084A 00000000 PlugIn Data 8FC20010 0002102B 304200FF 8FC30010 2463FFFF AFC30010 1440FFEE PlugIn Data 00000000 AFC00000 0B400845 00000000 8FC20000 24420001 AFC20000 PlugIn Data 8FC30000 340284D0 0062102A 1440FFF9 00000000 03C0E821 8FBE000C PlugIn Data 27BD0010 03E00008 00000000 Setpin 1, dout Setpin 2, dout pin(1)=1 pin(2)=1 A$ = chr$(TxData) Plugin A$ But I wonder why you want to make this into a plugin? The BASIC code that you have is quite workable. For others, I have found that the compiler seems to move the start of the code around. So check the disassembly listing to make sure that that the first byte of the plugin starts at 0x9D002000 and adjust line 115 of PlugIn.ld if it is not correct. In the meantime I will work on a more reliable solution. Geoff Geoff Graham - http://geoffg.net |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Hi Geoff, Thanks for your response. One assumption you made is that I needed it for a PlugIn. Well I actually just need the C translation so your example you gave is good - thanks. That said, at least I can also work from your example as to how to create a PlugIn which was a question I posted on the other thread. Any other "C" translations welcome - I learn from example and know there are many solutions to the initial request. Thanks Geoff - something to study now to solve two issues! ![]() WW |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3272 |
Ah, sorry, I've been plugin focused for the last three days. In that case you have some work ahead of you. Especially with controlling the I/O pins as every chip/manufacturer/IDE does it differently. Geoff Geoff Graham - http://geoffg.net |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Your 'C translation' you sent me gets me past my next hurdle so it is much appreciated. I'm already able to control I/Os on the PIC24F that I am using in C. However, your PlugIn feature now potentially opens up the MicroMite for the project once again. Either way, whether I end up using the PIC24 or the MicroMite with a PlugIn, I will need to learn C . . . . ![]() |
||||
Dylan Regular Member ![]() Joined: 17/06/2013 Location: NetherlandsPosts: 81 |
Don't be scared by C. It is a small language that does, admittedly, have a huge specification. Read K&R, and then don't actually program in it too much unless you also like hitting yourself in the face, but don't be scared. It only takes half an hour to compile on/for Windows :) |
||||
paceman Guru ![]() Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
What or who is K&R ? |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Kernighan & Ritchie the fathers of C and UNIX The only Konstant is Change |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9502 |
As a slight digression, why did they call it "C"? Smoke makes things work. When the smoke gets out, it stops! |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3272 |
From what I remember the first language that ran on UNIX was called "B" and when the the researchers at Bell Labs created a new language they (logically?) called it "C". Why was the first language called "B"? The exact answer to that is lost in the mists of time. K&R is "The C Programming Language" (often called the 'white book') which is a book written by Brian Kernighan and Dennis Ritchie describing the language. It is a masterpiece of clear and understandable writing and is still in print today. I recommend it to anyone who wants to get into C. As a footnote I met Brian Kernighan in the '90s and got to buy him lunch. My only brush with fame. Geoff Geoff Graham - http://geoffg.net |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |