c - PIC32 - PWM 和正弦波

标签 c microchip trigonometry pwm pic32

我正在尝试编写一个程序来生成正弦波,我将通过被动夏季输出这些正弦波,从而在我按下按钮时让微 Controller 播放声音。

我已经让 PWM 正常工作,并且可以使用常量值演奏音符,但我想使用正弦波而不是常量。

这是我到目前为止提出的代码:

#include <pic32mx.h>
#include <stdint.h>

extern void _enable_interrupt();

//Change length of the Steps.
#define step 0.00000000000000000001


struct Note{
int number;
int freq;
double A;
double x;
int play;
}Note;

struct Note array[12];

void gen(int i){
 float pi = 3.141592;
 double x = array[i].x;
 double freq = array[i].freq;
 double A = sin(freq*x*step*2*pi);
 x++;
 array[i].A = A;
 array[i].x = x;
}

void initNotes(){



 struct Note C = {0, 200, 0.0, 0.0, 0};
 struct Note Ciss = {1, 200, 0.0, 0.0, 0};
 struct Note D = {2, 200, 0.0, 0.0, 0};
 struct Note Diss = {3, 4100, 0.0, 0.0, 0};
 struct Note E = {4, 3900, 0.0, 0.0, 0};
 struct Note F = {5, 3700, 0.0, 0.0, 0};
 struct Note Fiss = {6, 3500, 0.0, 0.0, 0};
 struct Note G = {7, 3300, 0.0, 0.0, 0};
 struct Note Giss = {8, 3100, 0.0, 0.0, 0};
 struct Note B = {9, 2900, 0.0, 0.0, 0};
 struct Note Biss = {10, 2700, 0.0, 0.0, 0};

 array[0] = C;
 array[1] = Ciss;
 array[2] = D;
 array[3] = Diss;
 array[4] = E;
 array[5] = F;
 array[6] = Fiss;
 array[7] = G;
 array[8] = Giss;
 array[9] = B;
 array[10] = Biss;


}
void initSynth() {
 for(;;) {
  int btns = getBtns();

  // Check buttons. If button is pressed, corresponding note's play-value will be set to 1 (true)
  if((PORTD & 0b000000100000) == 0b000000100000){
       array[0].play = 1; 

  }else{
        array[0].play = 0;
  }
  if((PORTD & 0b000001000000) == 0b000001000000){
            array[1].play = 1;
  }
  else{
      array[1].play = 0;
  }
  if((PORTD & 0b000010000000) == 0b000010000000){
            array[2].play = 1;
  }else {
      array[2].play = 0;
  }
  // Check note-array for notes with play-value 1 (true). These are added together.

  int c;
  int nr;
  int i;
  for(i = 0; i < 11; i++){
   if(array[i].play == 1){
    c = c + array[i].A;
    gen(i);
    nr++;
   }
        }

  // Set PWM to previously calculated value.
  if((c/nr) > 0){
        setPwm((c/nr), 50);
  } else setPwm(0,0);
 }
}

int getBtns(void) {
  return((PORTD >>5) &0x7); /* Port D bits 5 through 8 is used for the Buttons and is set to 1 (input) */
 }

 int getSwitches(void) {
  return((PORTD>>8) &0xF); /* Port D bits 8 through 12 is used for the Switches and is set to 1 (input) */
 }

 int getbtn(void) {
  return(PORTD &0x1);
 }


void initPwm(){
 T2CON = 0x070; // Clear timer2, prescale at 1:1
 TMR2 = 0x0; // Timer2 value starts at 0
 OC1CON = 0x0000; // Turn off and clear pwm
 OC1R = 0x0001;
 OC1RS = 0; // Dutycycle
 OC1CON = 0x0006; // Configure for PWM mode without Fault pin
 PR2 = 0; // Set dutycycle, HÄR ÄNDRAR MAN TONER!
 IECSET(0) = 0x0100; // Enable T2 interrupt
 IPCSET(2) = 0x01C; // Set T2 interrupt priority to 7
 enable_interrupt();
 OC1CONSET = 0x08000; // Enable OC1
 T2CONSET |= 0x08000; // Enable Timer2
}

void setPwm(int pwm, int duty){
    int dutycycle = 0xFFFFFFFF * (duty / 100);
 OC1RS = duty;
 PR2 = pwm;
}

int main(void) {
 TRISE = 0x00; /* Port E bits 0 through 7 is used for the LED and is set to 0 (output) */
 PORTE = 0x00;
 initNotes();
 initPwm();
 initSynth();
 return 0;
}

void user_isr( void ) {
 if((IFS(0)&0x0100)==0x0100){
  IFSCLR(0) = 0x0100;
 }
}

当我按下按钮时,每次按下按钮时,夏天开始以不同的速度发出嗡嗡声和滴答声。有时我得到干净的音调,所以我觉得我有点接近解决方案。是计算机运行速度变慢的问题吗?也许解决方案是在某处添加另一个计时器?

最佳答案

很有可能您运行 PIC 的速度不够快,无法听到提示音。您的 PWM 速率可能在低 kHz 或 Hz 范围内,因此听起来很糟糕。阅读 OSC 系列引用手册并使用配置位将您的 PIC 设置为 80MHz(如果您使用的是 PIC32MX)。 http://ww1.microchip.com/downloads/en/DeviceDoc/61112H.pdf

关于c - PIC32 - PWM 和正弦波,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41969364/

相关文章:

java - 计算 3D 椭圆轨道上的点

c++ - 在这种情况下, 'addition' 和 'bitwise or' 是否相同?

c - 为什么 clang 需要 -lm 不像 gcc?

c - 使用中断处理程序时如何避免使用全局变量?

python - 使用 python 在值列表上的余弦距离

c++ - 基准三角查找表性能提升与 cpp 实现

c - 计算英尺和英寸为浮点,然后执行英制到公制转换功能

c++ - C/C++ 的数据库写入与磁盘 I/O

c - 为什么 Timer1 不在 PIC18 上计数?

assembly - PIC16F887 Timer0计数溢出