stm32f103c8t6控制max7912 的8x8的音乐点阵频谱灯的程序
时间: 2023-08-10 21:02:56 浏览: 189
以下是一个简单的程序,使用STM32F103C8T6控制MAX7912芯片,实现8x8的音乐点阵频谱灯的控制:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define MAX7912_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define MAX7912_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
// 配置SPI1接口的GPIO管脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置片选CS的GPIO管脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置SPI1接口
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void MAX7912_Write(uint8_t reg, uint8_t value)
{
MAX7912_CS_LOW();
SPI1->DR = reg;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SPI1->DR = value;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
MAX7912_CS_HIGH();
}
void MAX7912_Init(void)
{
MAX7912_Write(0x0F, 0x00); // 关闭显示测试模式
MAX7912_Write(0x0C, 0x01); // 打开普通模式
MAX7912_Write(0x0B, 0x07); // 设置扫描限制,扫描所有的行
MAX7912_Write(0x09, 0xFF); // 设置亮度
MAX7912_Write(0x0A, 0x0F); // 设置显示的数量为8个
}
void MAX7912_Clear(void)
{
uint8_t i;
for (i = 1; i <= 8; i++) {
MAX7912_Write(i, 0x00);
}
}
void MAX7912_Display(uint8_t *buffer)
{
uint8_t i;
for (i = 1; i <= 8; i++) {
MAX7912_Write(i, buffer[i-1]);
}
}
void TIM2_IRQHandler(void)
{
static uint16_t counter = 0;
static uint8_t buffer[8] = {0};
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// 读取ADC值
uint16_t adc_value = ADC_GetConversionValue(ADC1);
// 计算音乐点阵频谱灯的显示值
uint8_t i, j;
for (i = 0; i < 8; i++) {
uint8_t value = 0;
uint16_t threshold = 256 * (i + 1) / 8;
for (j = 0; j < 32; j++) {
uint16_t sample = ADC_GetConversionValue(ADC1);
if (sample > threshold) {
value |= 1 << j;
}
}
buffer[i] = value;
}
// 显示音乐点阵频谱灯
MAX7912_Display(buffer);
counter++;
if (counter == 500) {
printf("ADC value = %d\n", adc_value);
counter = 0;
}
}
}
int main(void)
{
// 初始化SPI1接口和MAX7912芯片
SPI1_Init();
MAX7912_Init();
// 初始化ADC
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
// 配置ADC的GPIO管脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置ADC
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
// 初始化定时器
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while (1) {
// 循环等待中断发生
}
}
```
请注意,本程序只是一个简单的例子,可能需要根据您的具体应用做出一些修改。此外,如果您需要使用其他型号的MAX7912芯片,可能需要根据数据手册更改寄存器的值。
阅读全文