Continue to Site

# how i can implement output_b(SINE_WAVE[i]) in mikroc

Status
Not open for further replies.

### how i can implement output_b(SINE_WAVE) in mikroc​

I have found a code written in CCS C.My question is how i can implement it in mikroc

Code:
int i
CONST unsigned int SINE_WAVE[30] = {
0b00010000, 0b10010000, 0b01010000, 0b11010000, 0b00110000, 0b10110000,
0b01110000, 0b11110000, 0b01110000, 0b10110000, 0b00110000, 0b11010000,
0b01010000, 0b10010000, 0b00010000, 0b11100000, 0b01100000, 0b10100000,
0b00100000, 0b11000000, 0b01000000, 0b10000000, 0b00000000, 0b10000000,
0b01000000, 0b11000000, 0b00100000, 0b10100000, 0b01100000, 0b11100000};
main()
{
for(i=0;i<30;i++)
{
output_b(SINE_WAVE[i]);
}
}

Here how i can implement output_b(SINE_WAVE); function in mikroc

#### ZASto

So, where is your CCS C function?

#### khatus

##### Member level 2
i want to convert this code to mikroc

Code:
#include <16F88.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,PUT,CCPB3
#use delay(clock=8000000)

int i, j;

CONST unsigned int SINE_WAVE[30] = {
0b00010000, 0b10010000, 0b01010000, 0b11010000, 0b00110000, 0b10110000,
0b01110000, 0b11110000, 0b01110000, 0b10110000, 0b00110000, 0b11010000,
0b01010000, 0b10010000, 0b00010000, 0b11100000, 0b01100000, 0b10100000,
0b00100000, 0b11000000, 0b01000000, 0b10000000, 0b00000000, 0b10000000,
0b01000000, 0b11000000, 0b00100000, 0b10100000, 0b01100000, 0b11100000};

#INT_RTCC
void main_wave() {
if (i++ == 29)
i = 0;
output_b(SINE_WAVE);
}

#INT_TIMER2
void TIMER2_isr() {
if (j++ == 29)
j = 0;
output_a(SINE_WAVE[j]>>4);
}

void main(void) {
i = 0;
j = 0;
setup_adc_ports(NO_ANALOGS);    // Turn off analogue inputs
setup_oscillator(OSC_8MHZ | OSC_INTRC);   // Use internal 8MHz osc
set_timer0(0);

setup_counters( RTCC_INTERNAL, RTCC_DIV_1 | RTCC_8_BIT);

setup_timer_2( T2_DIV_BY_4, 65, 1 );
// 65 gives 250.8Hz

enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

while(TRUE)
{
delay_ms(1);
}

disable_interrupts(INT_RTCC);
}

Last edited by a moderator:

#### kgavionics

##### Full Member level 3
I only know standard C, but I think the output_a is just a function to send the values of the table to a single pin. You can use either bit banging or another serial protocol to send the value out!

#### FvM

##### Super Moderator
Staff member
but I think the output_a is just a function to send the values of the table to a single pin.
No. It's a build-in compiler function sending an 8-bit number to port A pins. The code assumes a 8-bit parallel DAC connected to the port.

#### kgavionics

##### Full Member level 3
No. It's a build-in compiler function sending an 8-bit number to port A pins. The code assumes a 8-bit parallel DAC connected to the port.
Thank you for the clarification, but why can't he use:
PORTn=(SINE_WAVE[j]>>4);

#### betwixt

##### Super Moderator
Staff member
... wouldn't that just output the upper 4 bits of the value and ignore the lower one?
I'm not familiar with MikroC, it doesn't run on my OS but periodically reading a table and sending its contents to a port is hardly rocket science.

Brian.

#### kgavionics

##### Full Member level 3
... wouldn't that just output the upper 4 bits of the value and ignore the lower one?
I'm not familiar with MikroC, it doesn't run on my OS but periodically reading a table and sending its contents to a port is hardly rocket science.

Brian.
Yeah, that what I thought too!
Sending or receiving data from port to another is one of the first step you learn in microcontroller world!
The problem, that so many people don't take the time to learn things from the ground up! And, that's why they use compilers that do most of the job for them, unlike assembler or standard C!

#### paulfjujo

hello,

where is the init of PORTA ?

TRISA=0;
PORTA=(SINE_WAVE[j]>>4);

look at this 16F88 MikroC version ... not tested

#### Attachments

• 16F88_example.c.txt
2.3 KB · Views: 31

#### betwixt

##### Super Moderator
Staff member
In the original code and the 16F88 example only four bits are used. The '>>4' part of the code just moves the upper bits to the lower bit positions. How that is used in the circuit depends on how the DAC is wired. If you really only want to use 4 bits, change the look up table and then you can omit the bit shift in software.

Note that a 4 bit sine wave only has 16 levels so it will look more like a staircase than a smooth curve. As all the port bits are used, why not also increase accuracy by using all the bits in the look-up table?

Brian.

#### paulfjujo

Hello,

you defined timers by frequency ! it is not usual
Timers are defined by time units !
so i started ,in my example , with 4mS periode for timers (because ~250Hz indication)
in this case , to explore the 30 steps => 30x4=120mS
it means Sinus defined for 8.33Hz
This frequency output is enough low to use
=> PWM instead of DAC ....

Other MCU , as PIC16F1615-16F1619 has a 8 bits DAC output 256 levels
18F26K22 5bit DAC outputs 32 levels

Status
Not open for further replies.