mikroC

making it simple...

Library Example

mikroC - C Compiler for Microchip PIC microcontrollers

The example is a simple demonstration of how to use sound library for playing tones on a piezo speaker. The code can be used with any MCU that has PORTB and ADC on PORTA. Sound frequencies in this example are generated by reading the value from ADC and using the lower byte of the result as base for T (f = 1/T).

int adcValue;

void main() {

PORTB

= 0;

// Clear PORTB

TRISB

= 0;

// PORTB is output

INTCON = 0;

// Disable all interrupts

ADCON1 = 0x82;

// Configure VDD as Vref, and analog channels

TRISA

= 0xFF;

// PORTA is input

Sound_Init(PORTB, 2);

// Initialize sound on RB2

while (1) {

// Play in loop:

adcValue = ADC_Read(2);

//

Get lower byte from ADC

Sound_Play(adcValue, 200);

//

Play the sound

}

 

 

 

}

 

 

page

 

MikroElektronika: Development tools - Books - Compilers

265