Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to convert this C code to mikroC?

Status
Not open for further replies.

bluemonday

Newbie level 6
Joined
May 4, 2007
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,395
hello.. i got this program which is written in C.. can i compile this using mikroC? or maybe it needs to be converted to mikroC declerations??

#include <16F877A.h>
#device *=16
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#byte PORTE = 0x09 // PortE lives in File 9

/* Note 20MHz clock must be used for 115,000 bps, the % error is to large at slower speeds */
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit = PIN_C6, rcv = PIN_C7, parity = N, bits = 8)

#use fast_io(E) // Fast access to PortE (don't fiddle with TRISE)
long int TimeBase;
long int TimeBaseMUX;
char bSample;


/* Forward declaration of functions */
void SetBaudRate();
void Sample_RealTime();

/** TimeBase Interrupt called every 20uS that's 50KHz MAX **/
#INT_TIMER2
Timer2_ISR()
{
// Sample Frequency = TimeBaseMUX * 20uS, e.g. 100Hz (1mS) = 20uS * 500
if (--TimeBase==0)
{
TimeBase = TimeBaseMUX;
bSample = TRUE;
}

}

main()
{
bSample = FALSE;

set_tris_e(0x17); // TRISE = 00010111; RE2,RE1 and RE0 TTL Inputs
SetBaudRate();

setup_adc_ports(A_ANALOG); // RA0 - RA4 Analog, RE0 - RE2 digital
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);

delay_us(20); // Delay for sampling cap to charge


/** Setup Time-Base timer2 **/ // 1/(20,000,000/(4*1*100*1)) = 20uS
setup_timer_2 (T2_DIV_BY_1,100,1); // interrupt every 20us, thats 50Khz max
set_timer2(0);
enable_interrupts(INT_TIMER2);

//Modified: 3/6/06
//TimeBaseMUX = 50; // 1000 Hz
TimeBaseMUX = 10; // 5000 Hz
TimeBase = TimeBaseMUX; // e.g. 50000 / 100 for 100 Hz (thats 500 * 20us = 10mS)

enable_interrupts(GLOBAL);

while(TRUE)
{
if(bSample == TRUE)
{
bSample = FALSE;
Sample_RealTime();
}
}
}

void Sample_RealTime()
{

// 20/03/2002. verision 1.0. sample ch1 to ch4, Chop Mode

// NOTE: by default in CCS all var's are unsigned
long int adcValue; // 16-bit storage for ADC reading
char adcHI,adcLO; // 8-bit storage for real-time frames.

/*** Channel 1 ***/
adcValue = read_adc(); // Get ADC reading

/* Convert 16-bit adcValue to Real-time frame structure, CH1 */
adcHI = (char)((adcValue >> 5)& 0x1f); // 0|0|0|d9|d8|d7|d6|d5
adcLO = (char)((adcValue & 0x1f)|0x80); // 1|0|0|d4|d3|d2|d1|d0

putc(adcHI); // Transmit Byte 1 (d9...d5)
putc(adcLO); // Transmit Byte 2 (d4...d0)


/*** Channel 2 ***/
set_adc_channel(1);
delay_us(20); // Delay for sampling cap to charge

adcValue = read_adc(); // Get ADC reading

/* Convert 16-bit adcValue to Real-time frame structure, CH2 */
adcHI = (char)(((adcValue >> 5)& 0x1f)|0x20); // 0|0|1|d9|d8|d7|d6|d5
adcLO = (char)((adcValue & 0x1f)|0xA0); // 1|0|1|d4|d3|d2|d1|d0

putc(adcHI); // Transmit Byte 1 (d9...d5)
putc(adcLO); // Transmit Byte 2 (d4...d0)

//-----------------------Modified: 3/6/06---------------------------------
/*** Channel 3 ***/
//set_adc_channel(2);
//delay_us(20); // Delay for sampling cap to charge

//adcValue = read_adc(); // Get ADC reading

/* Convert 16-bit adcValue to Real-time frame structure, CH3 */
//adcHI = (char)(((adcValue >> 5)& 0x1f)|0x40); // 0|1|0|d9|d8|d7|d6|d5
//adcLO = (char)((adcValue & 0x1f)|0xC0); // 1|1|0|d4|d3|d2|d1|d0

//putc(adcHI); // Transmit Byte 1 (d9...d5)
//putc(adcLO); // Transmit Byte 2 (d4...d0)


/*** Channel 4 ***/
//set_adc_channel(3);
//delay_us(20); // Delay for sampling cap to charge

//adcValue = read_adc(); // Get ADC reading

/* Convert 16-bit adcValue to Real-time frame structure, CH4 */
//adcHI = (char)(((adcValue >> 5)& 0x1f)|0x60); // 0|1|1|d9|d8|d7|d6|d5
//adcLO = (char)((adcValue & 0x1f)|0xE0); // 1|1|1|d4|d3|d2|d1|d0

//putc(adcHI); // Transmit Byte 1 (d9...d5)
//putc(adcLO); // Transmit Byte 2 (d4...d0)
//------------------------------------------------------------------------

/** Back to Channel 1 */
set_adc_channel(0);
delay_us(20); // Delay for sampling cap to charge

}
void SetBaudRate()
{
switch(PORTE & 0x07) // Read dip switches and setup baud rate
{
case 0: set_uart_speed(4800); break;
case 1: set_uart_speed(9600); break;
case 2: set_uart_speed(14400); break;
case 3: set_uart_speed(19200); break;
case 4: set_uart_speed(32768); break;
case 5: set_uart_speed(38400); break;
case 6: set_uart_speed(57600); break;
case 7: set_uart_speed(115200); break;
}
}

can anyone tell me how to convert this code to mikroC? please help...
 

how to set the bits in mikroc

it's CCS, it have many functions to work with the PIC... mikroC have similar function for USART (instead of putc use Usart_write) for setting the timers, you should do it manually... also you should set manually the ADC registers to get that config... in mikroC it uses RC-clock.... well .it should be....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top