làm bài happy birth day với stm8s nào

tdm

Kỹ sư
#1
code:
Mã:
//out put PD4
#include "stm8s.h"
#include <iostm8s003f3.h>//copy vao thu muc chua code xoa dong (//  unsigned char OPT         : 1;)thi dich moi khong bao loi
#include <stm8s_it.h>
#define A_NOTE_4 4562//262hz 
#define Ab_NOTE_4 4830//
#define B_NOTE_4 4070  //
#define Bb_NOTE_4 4298  //
#define C_NOTE_4 7658
#define C_NOTE_5 3828
#define D_NOTE_4 6830
#define Db_NOTE_4 6430
#define E_NOTE_4 6090
#define Eb_NOTE_4 6450
#define F_NOTE_4 5740
#define G_NOTE_4 5120
#define Gb_NOTE_4 4298
//      523hz   1047hz    2093hz
const long C_NOTE[4]  ={7658,     3828,   1918,    958};
                    //   277hz     554hz    1109hz    2217hz
const long Db_NOTE[4] ={ 277,     554,    1109,    2217};//D#4
                      //294,     587,    1175,    2349
const long D_NOTE[4]  ={6830,     587,    1175,    2349};

const long Eb_NOTE[4] ={ 6450,     622,    1245,    2489};//D#4
                     //330,     659,    1329,    2637
const long E_NOTE[4]  ={ 6090,     659,    1329,    2637};
                     // 349,     698,    1397,    2794
const long F_NOTE[4]  ={ 5740,     698,    1397,    2794};
const long Gb_NOTE[4] ={ 5420,     740,    1480,    2960};//G#4
const long G_NOTE[4]  ={ 5120,     784,    1568,    3136};
const long Ab_NOTE[4] ={ 4830,     831,    1661,    3322};//A#4
const long A_NOTE[4]  ={ 4562,     880,    1760,    3520};
const long Bb_NOTE[4] ={ 4298,     923,    1865,    3729};
const long B_NOTE[4]  ={ 4070,     988,    1976,    3951};//B#4
#define SIZE 25
#define tempo 75
u16 happy_bday[SIZE] = {
C_NOTE_4,C_NOTE_4,D_NOTE_4,C_NOTE_4, F_NOTE_4, E_NOTE_4,
C_NOTE_4, C_NOTE_4, D_NOTE_4, C_NOTE_4, G_NOTE_4, F_NOTE_4,
C_NOTE_4, C_NOTE_4, C_NOTE_5, A_NOTE_4, F_NOTE_4, E_NOTE_4, D_NOTE_4,
Bb_NOTE_4, Bb_NOTE_4, A_NOTE_4, F_NOTE_4, G_NOTE_4, F_NOTE_4};  
///////////////////////////////////////////////////////////////////
uint16_t truong_do[SIZE] = {
350,100,500,500, 500, 900,
350,100,500,500, 500, 900,
350,100,500,500, 500, 500, 900,
350,100,700,1000, 1500,2100};

//////////////////////////////////////////
void delay_us(unsigned int time)
{
   unsigned int i;
   for(i=0;i<time;i++)
   {
      ;// khong lam gi ca
   }
}
void delay_ms(unsigned int time)
{
    unsigned int i,j;
    for(i=0;i<time;i++)
    for(j=0;j<380;j++)//f=2mhz=380; f=16mhz=3040
    {
      ;// khong lam gi ca
    }
}
void capture_2(void){}
//////////////////////////////////////////
void InitGPIO(void)
{
  PA_DDR = 0xF0;
  PA_CR1 = 0xfF;
  PC_DDR = 0xFF;
  PC_CR1 = 0xFF;
  PD_DDR = 0xFF;
  PD_CR1 = 0xFF;
  PB_DDR = 0xFF;
  PB_CR1 = 0xFF;
}
/////////////////////////////////////
static void TIM2_PWM1(uint16_t CCR1_Val_2)
{
  /* Time base configuration */
 // TIM2_TimeBaseInit(TIM2_PRESCALER_1,F_NOTE[0] )
                                                //F=2MHZ==>2000000/262hz=7634=note C
  /* PWM1 Mode configuration: Channel1 */
  TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val_2, TIM2_OCPOLARITY_LOW);//HIGH LOW
  TIM2_OC1PreloadConfig(ENABLE);
  TIM2_ARRPreloadConfig(ENABLE);

  /* TIM2 enable counter */
  TIM2_Cmd(ENABLE);
}
 void main(void)
{
  
    InitGPIO();
    while(1){
       u16 i;
     for(i=0;i<SIZE;i++)
     {
        TIM2_TimeBaseInit(TIM2_PRESCALER_1,happy_bday[i] );TIM2_PWM1(( happy_bday[i] /2));delay_ms(truong_do[i]);
        TIM2_PWM1(0); delay_ms(tempo);
     }
   
   
    }}



#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{

}
}
#endif
 

Quảng cáo Google