Menu

all about electronic and microcontrollers

Tuesday, December 14, 2010

Lesson nr.20-Sound generation

Hardware setup:


Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.20:
'          Sound generation.
'  Done by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with sounds.
'          I'll show how to generate and make sound helped by microcontroller 
'          and some buttons.
'          Communication with the buzzer will be performed  at PORT RB3 ( as PWM mode).
'          The Push buttons are connected to PORT RA4 (generates Tone1), RA6
'          (generates Tone2) and RA7(generates melody2). 
'          Of course we need pull-up resistors (4k7 is required).
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
void Tone1()
{
 Sound_Play(5000, 1000);         // Frequency = 659Hz, duration = 250ms
}

void Tone2() 
{
 Sound_Play(4000, 1000);         // Frequency = 698Hz, duration = 250ms
 }

void Tone3() 
{
 Sound_Play(3000, 1000);         // Frequency = 784Hz, duration = 250ms
 }

void Melody1()                  // Make funny melody 1;
{
 Tone1(); Tone2(); Tone3();
 Tone3(); Tone2(); Tone1();
 Tone1(); Tone3(); Tone2();
 Tone3(); Tone2(); Tone1(); Tone3();
 Tone1(); Tone2(); Tone3();
 Tone3(); Tone1(); Tone2(); Tone2(); Tone1();
 }

void ToneA()                    // Tone A;
{
 Sound_Play(1618, 50);          // (frequency, duration)
}

void ToneB()                    // Tone B;
{
 Sound_Play(1918, 50);          // (frequency, duration)
}

void ToneC()                    // Tone C;
{
 Sound_Play(2212, 50);          // (frequency, duration)
}

void Melody2()                  // Make funny melody 2;
{
 unsigned short i;
 for (i = 12; i > 0; i--)
 {
  ToneA(); ToneB(); ToneC();
  }
 }

void main() 
{
 CMCON |=7;                     // Disable comparator
 TRISB = 0xF0;                  // Pins RB7-RB4 are configured as inputs,
                                // RB3 is configured as an output
 Sound_Init(&PORTB, 3);
 Sound_Play(5000,100);          // Start with this song (frequency, duration)

 while (1)                      // infinite loop
 {
  if (Button(&PORTA,4,1,0))     // RB4 generates Tone1
  Tone1();
  
  if (Button(&PORTA,6,1,0))     // RB6 generates Tone2
  Tone2();

  if (Button(&PORTA,7,1,0))     // RA7 generates melody 2
  {
   Melody2();
   Delay_ms(1500);              // Delay 1,5s
   Melody2();
   Delay_ms(1500);              // Delay 1,5s
   Melody2();
   }
  }
}

Lesson nr.19-Using Internal EEPROM Memory

Hardware setup:



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.19:
'          Using Internal EEPROM Memory.
'  Done by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with internal EEPROM Memory
'          and I'll show how to write and read information on it helped by a little
'          menu displayed on the LCD.
'          Communication with LCD will be performed through 4-bits and connections
'          is made as follows: D4 with RB0, D5 with RB1, D6 with RB2, D7 with RB3;
'                              RS with RB4 and EN with RB5.
'          The Push buttons are connected to PORT RA4 (for read), RA6(for write) and RA7
'          (for delete). Of course we need pull-up resistors (4k7 is required).
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// LCD module connections
sbit LCD_RS at RB4_bit;                 // LCD_RS assigned to PORT RB4;
sbit LCD_EN at RB5_bit;                 // LCD_EN assigned to PORT RB5;
sbit LCD_D4 at RB0_bit;                 // LCD_D4 assigned to PORT RB0;
sbit LCD_D5 at RB1_bit;                 // LCD_D5 assigned to PORT RB1;
sbit LCD_D6 at RB2_bit;                 // LCD_D6 assigned to PORT RB2;
sbit LCD_D7 at RB3_bit;                 // LCD_D7 assigned to PORT RB3;

sbit LCD_RS_Direction at TRISB4_bit;    // LCD_RS assigned to TRIS B4;
sbit LCD_EN_Direction at TRISB5_bit;    // LCD_EN assigned to TRIS B5;
sbit LCD_D4_Direction at TRISB0_bit;    // LCD_D4 assigned to TRIS B0;
sbit LCD_D5_Direction at TRISB1_bit;    // LCD_D5 assigned to TRIS B1;
sbit LCD_D6_Direction at TRISB2_bit;    // LCD_D6 assigned to TRIS B2;
sbit LCD_D7_Direction at TRISB3_bit;    // LCD_D7 assigned to TRIS B3;
// End LCD module connections

// Define Messages
char message1[] = "Switch1: Read";
char message2[] = "Switch2: Write";
char message3[] = "Switch3: Erase";
char message4[] = "EEPROM_Write";
char message5[] = "EEPROM_Read ";
char message6[] = "EEPROM ERASED";
char message7[] = "Internal";
char message8[] = "EEPROM";
char message9[] = "*";
char message10[] = "**";
char message11[] = "***";

char digit[] = "00000000000000";
unsigned short  ii, All ;

void read_eeprom()
{
 if (Button(&PORTA, 4, 1, 0))           // Detect logical one to zero;
 {    
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message5);
  Lcd_Out(2,7,message9);
  delay_ms(1000);                       // Delay 1s
  Lcd_Out(2,7,message10);
  delay_ms(1000);                       // Delay 1s
  Lcd_Out(2,7,message11);
  delay_ms(1000);                       // Delay 1s
  for (ii=0; ii<=13; ii++)
  {
   All = EEPROM_Read(0x00+ii);          // Start EEPROM Location,
   digit[ii] = All+65;                  // Look at ascii table
   }
  Lcd_Out(2,2,digit);
  delay_ms(5000);                       // Delay 5s
  }
 }

void write_eeprom()
{
 if (Button(&PORTA, 6, 1, 0))           // Detect logical one to zero
 {
  for (ii=0; ii<14; ii++)
  EEPROM_Write(0x00+ii,ii);
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message4);
  Lcd_Out(2,7,message9);
  delay_ms(1000);
  Lcd_Out(2,7,message10);
  delay_ms(1000);
  Lcd_Out(2,7,message11);
  delay_ms(1000);
  }
 }

void delete_eeprom()
{
 if (Button(&PORTA, 7, 1, 0))           // Detect logical one to zero
 {     
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message6);
  Lcd_Out(2,7,message9);
  delay_ms(1000);
  Lcd_Out(2,7,message10);
  delay_ms(1000);
  Lcd_Out(2,7,message11);
  delay_ms(1000);
  for (ii=0; ii<=13; ii++)
  EEPROM_Write(0x00+ii,0x0e);
  }
 }

void main() {
CMCON  |= 7;                            // Disable Comparators
TRISA4_bit = 1;
TRISA6_bit = 1;
TRISA7_bit = 1;
TRISB = 0;
PORTB = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);                    // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off
Lcd_Out(1,5,message7);
Lcd_Out(2,6,message8);
delay_ms(3000);                         // Delay 3s
do {
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message1);              // Write message1 in 1st row
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message2);
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message3);
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    } while(1);
}