| View previous topic :: View next topic |
| Author |
Message |
Samman
Joined: 25 Jan 2008 Posts: 2
|
Posted: Thu Jan 31, 2008 7:45 pm Post subject: Convert from AVR C to MikroC |
|
|
Hi there!
Is there any kond soul out there that can help me modify a AvrC code to MikroC?
And also make a small modifikation?
Please  |
|
| Back to top |
|
BrunoG Site Admin
Joined: 22 Nov 2005 Posts: 636
|
Posted: Tue Feb 05, 2008 6:25 pm Post subject: |
|
|
Hi Samman,
Can you be more specific about the small modification you are talking about
This will help you to get answers _________________ BrunoG, Administrator |
|
| Back to top |
|
Samman
Joined: 25 Jan 2008 Posts: 2
|
Posted: Thu Feb 07, 2008 7:14 pm Post subject: |
|
|
Hi Bruno. Yes, the code is written for AVR but i want it to work for a PIC instead. The code read the data from INT1 (433 Mhz receiver) and then send it to RS232.
I'm goint to use a PIC18F452 and a 8 Mhz crystal.. I have just bought MicroC and i can't figure out how to rewrite this code..
The code is
| Code: |
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
//####### CONFIG ########
//Note: if you use another xtal, you need to adjust the BITTIMEx values
#define F_CPU 4433619UL
#define BAUD 38400
#define MAXBITS 50 //max bits to receive
#define BITTIME0 68 //length of a 0 pulse
#define BITTIME1 21 //length of a 1 pulse
#define TOL 10 //length tolerance
//#define DEBUG //show me the bit-lengths
//#######################
//some useful stuff
#define cbi(reg, bit) (reg &= ~_BV(bit))
#define sbi(reg, bit) (reg |= _BV(bit))
#define uart(d) loop_until_bit_is_set(USR, UDRE); UDR = d;
//INT1 defines
#define INT_FALLING_EDGE sbi(MCUCR, ISC11); cbi(MCUCR, ISC10);
#define INT_RAISING_EDGE sbi(MCUCR, ISC11); sbi(MCUCR, ISC10);
#define INT_ENABLE sbi(GIMSK, INT1); // enable INT1
#define INT_DISABLE cbi(GIMSK, INT1); // disable INT1
#define INT_IS_RISING_MODE (MCUCR & (1<<ISC10))
volatile uint8_t bittime[MAXBITS];
volatile uint8_t bit[MAXBITS];
volatile uint8_t bitpos=0;
#ifdef DEBUG
static char hex[]="01234567890ABCDEF";
void dec2Uart(uint8_t c) {
uart((c/100)+'0');
uart((c/10%10)+'0');
uart((c%10)+'0');
}
#endif
//T0 Overflow
SIGNAL (SIG_OVERFLOW0) {
INT_DISABLE;
TCCR0=0; // Stop Timer
uint8_t c;
uint8_t ok=1;
uint8_t i;
for (i=0;i<bitpos;i++) {
c='-';
//check for 1-bit
if (bittime[i]>(BITTIME1-TOL) && bittime[i]<(BITTIME1+TOL)) {
bit[i]=1;
c='1';
}
//check for 0-bit
if (bittime[i]>(BITTIME0-TOL) && bittime[i]<(BITTIME0+TOL)) {
bit[i]=0;
c='0';
}
if (c=='-') ok=0;
#ifdef DEBUG
//writes A:B=C to the UART, where A is the Number of the bit,
//B ist the pulse-length an C is the result.
dec2Uart(i);
uart(':');
dec2Uart(bittime[i]);
uart('=');
uart(c);
uart('\r');
#endif
}
for (i=1;i<bitpos;i+=2) {
uart(bit[i]+'0');
}
uart('\r');
for (i=0;i<bitpos;i+=2) {
uart(bit[i]+'0');
}
uart('\r');
#ifdef DEBUG
uart(13);
#endif
if (bitpos==44 && ok) {
// decode # of bits and transmit (Routine to write data)
uart(13);
} else {
// write x if decoding has failed
uart('x');
uart(13);
}
bitpos=0;
INT_RAISING_EDGE;
INT_ENABLE;
}
//INT1
SIGNAL (SIG_INTERRUPT1) {
INT_DISABLE;
if (INT_IS_RISING_MODE) {
INT_FALLING_EDGE;
} else {
if (bitpos<(MAXBITS-1)) {
bittime[bitpos++]=TCNT0;
}
INT_RAISING_EDGE;
}
INT_ENABLE;
sbi(GIMSK, INT1); // disable ext. int.
TCNT0=0;
TCCR0=_BV(CS01) | _BV(CS00);
}
int main (void) {
DDRB = 0b00000000; // 1=Output, 0=Input
PORTB = 0b00000000; // activate internal pull-up
DDRD = 0b00000000; // 1=Output, 0=Input
PORTD = 0b00000000; // activate internal pull-up
// UART
sbi(UCR,TXEN); //enable Uart-TX
UBRR=(F_CPU / (BAUD * 16L) - 1); //set Baudrate
// TIMER0
sbi(TIMSK, TOIE0); // enable T0 Overflow
sei(); //enable Interrupts
//GIFR = (1<<INTF1);
INT_ENABLE;
INT_RAISING_EDGE;
for (;;) {
}
}
|
After row with "// decode # of bits and transmit (Routine to write data)" i have removed 5 lines to decode data because i will not use the orginal data decoder. |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|