神秘的仪器,新的代码,背后的真相到底是。。。。

欢迎收看今天的“小狮子,带侦探”!上次我们在指挥官的卧室里发现了散发诡异光芒的小灯和一串神秘的代码。今天我们在指挥部后面的小花园里又发现了一个奇怪的装置,这个装置被遗弃在了灌木丛中,但是!我,最可爱的人型s.a.t.8依旧找到了这重要的线索!话不多说,让我们康康今天的带发现吧!

#include "stm32f10x.h"
#include "led.h"
void Delay(uint32_t jun){
  for(;jun!=0;--jun);
}
int main(void){
int a=1,b=0;
  led_gpio_sb();
  led_gpio_yw();
while(1){
  while(a==1){
    if(key_scan(K1_GPIO_PORT,LED_K1_GPIO_PIN)==KEY_ON||b==1){
      led_gpio_config();
      led_gpio_JUN();	
      led_gpio_BIAN();
      while(a==1){		
    LED_G(ON);	 
    LED_B(OFF);	
    LED_R(OFF);
    Delay(0xFFFFF);	
        if(key_scan(K2_GPIO_PORT,LED_K2_GPIO_PIN)==KEY_ON){
      a=0;		
        LED_G(OFF);	 
        LED_B(OFF);
        LED_R(OFF);
      break;}
        LED_G(OFF);	 
        LED_B(ON);
        LED_R(OFF);
        Delay(0xFFFFF);	
        if(key_scan(K2_GPIO_PORT,LED_K2_GPIO_PIN)==KEY_ON){
      a=0;		
        LED_G(OFF);	 
        LED_B(OFF);
        LED_R(OFF);
      break;}
    LED_G(OFF);	 
    LED_B(OFF);
    LED_R(ON);
    Delay(0xFFFFF);	
      if(key_scan(K2_GPIO_PORT,LED_K2_GPIO_PIN)==KEY_ON){
      a=0;		
        LED_G(OFF);	 
        LED_B(OFF);
        LED_R(OFF);
      break;
        }     
      }
    }	
if(a==0) break;
  
  }
    if(key_scan(K1_GPIO_PORT,LED_K1_GPIO_PIN)==KEY_ON){
    a=1;
      b=1;
    
  }
}
}
#include "led.h"
void led_gpio_config(void){
  
  GPIO_InitTypeDef       GPIO_InitStruct;

  RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK, ENABLE);
  
  GPIO_InitStruct.GPIO_Pin=LED_G_GPIO_PIN;
  
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
  
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    
  GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  
}
void led_gpio_JUN(void){
  
  GPIO_InitTypeDef       GPIO_InitStruct;
  
  RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK, ENABLE);
    
  GPIO_InitStruct.GPIO_Pin=LED_B_GPIO_PIN ;
    
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
  
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  
  GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  
}
void led_gpio_BIAN(void){
  
  GPIO_InitTypeDef       GPIO_InitStruct;
  
  RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK, ENABLE);
  
  GPIO_InitStruct.GPIO_Pin=LED_R_GPIO_PIN ;
  
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
  
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  
  GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct);
  
}

void led_gpio_sb(void){
  
  GPIO_InitTypeDef       GPIO_InitStruct;
  
  RCC_APB2PeriphClockCmd(K1_G_GPIO_CLK, ENABLE);
  
  GPIO_InitStruct.GPIO_Pin=LED_K1_GPIO_PIN ;
  
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  
  GPIO_Init(K1_GPIO_PORT, &GPIO_InitStruct);
  
}

void led_gpio_yw(void){
  
  GPIO_InitTypeDef       GPIO_InitStruct;
  
  RCC_APB2PeriphClockCmd(K2_G_GPIO_CLK, ENABLE);
  
  GPIO_InitStruct.GPIO_Pin=LED_K2_GPIO_PIN ;
  
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    
  GPIO_Init(K2_GPIO_PORT, &GPIO_InitStruct);
  
}

uint8_t key_scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin){
  if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin)==KEY_ON){
    while(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin)==KEY_ON);
    return KEY_ON;
  }
  else return KEY_OFF;
}
#ifndef led_h
#define led_h
#include "stm32f10x.h"
#define LED_G_GPIO_PIN     GPIO_Pin_0
#define LED_R_GPIO_PIN     GPIO_Pin_5 
#define LED_B_GPIO_PIN     GPIO_Pin_1 
#define LED_G_GPIO_PORT    GPIOB
#define K1_GPIO_PORT    GPIOA
#define K2_GPIO_PORT    GPIOC
#define LED_G_GPIO_CLK     RCC_APB2Periph_GPIOB
#define K1_G_GPIO_CLK     RCC_APB2Periph_GPIOA
#define K2_G_GPIO_CLK     RCC_APB2Periph_GPIOC
#define LED_K1_GPIO_PIN     GPIO_Pin_0
#define LED_K2_GPIO_PIN     GPIO_Pin_13
#define ON   1
#define OFF  0
#define LED_G(JUN) if(JUN)GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);else GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
#define LED_B(JUN) if(JUN)GPIO_ResetBits(LED_G_GPIO_PORT, LED_B_GPIO_PIN);else GPIO_SetBits(LED_G_GPIO_PORT, LED_B_GPIO_PIN);
#define LED_R(JUN) if(JUN)GPIO_ResetBits(LED_G_GPIO_PORT, LED_R_GPIO_PIN);else GPIO_SetBits(LED_G_GPIO_PORT, LED_R_GPIO_PIN);
void led_gpio_config(void);
void led_gpio_JUN(void);
void led_gpio_BIAN(void);
void led_gpio_sb(void);
void led_gpio_yw(void);
uint8_t key_scan(GPIO_TypeDef *GPIO,uint16_t GPIO_Pin);
#define KEY_ON 1
#define KEY_OFF 0
#define LED_G_TOGGLE {LED_G_GPIO_PORT->ODR ^=LED_G_GPIO_PIN;}
#define LED_R_TOGGLE {LED_G_GPIO_PORT->ODR ^=LED_R_GPIO_PIN;}
#define LED_B_TOGGLE {LED_G_GPIO_PORT->ODR ^=LED_B_GPIO_PIN;}
#endif

乍一看,这个装置没什么亮点,但是当我们打开开关摁下key1按钮后发现这个装置上的led灯竟然开始闪烁了,当我们再摁下key2按钮后发现led灯竟然熄灭了。这次的发现比上次的更加劲爆,很显然指挥官的失踪并不是无端发生的,这次的装置和上次的装置一定是指挥官留下的线索。后勤官格林娜知道后第一时间来到了现场,在看过代码后,格林娜小姐说这次的代码和上次的有明显的不同,因为上次只有输出而这次却有了输入,通过对输入检测的编写可以让设备接受到信号并作出预先设计好的反应。

格林娜小姐在分析完决定调用格里芬指挥部的力量进行地毯式搜索,想要知道下一次的线索吗?那就继续关注“小狮子,带侦探”吧!我是最可爱的人形s.a.t.8,期待与您分享关于指挥官失踪的第一手资讯。

JQ8900的RTT[RT-thread]简易测试代码

-以下为示例代码

可以通过开发板上的按钮来对JQ8900进行操作,达到切换曲目的效果

并且可以自定义修改代码进行更多操作,优化了串口发送信息的方式。

/*
通过RTT例子修改
用于测试YSV0.7是否可用
*/

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>


#define LED0_PIN    GET_PIN(B, 1)    //定位到野火开发板上两个RGB相关的寄存器上
#define LED0_PINE   GET_PIN(B, 0)
#ifndef KEY0_PIN_NUM
    #define KEY0_PIN_NUM      GET_PIN(A, 0) /* PA0 */
#endif
#ifndef KEY1_PIN_NUM
    #define KEY1_PIN_NUM      GET_PIN(C, 13) /* PC13 */
#endif
#define SAMPLE_UART_NAME       "uart2"

static rt_device_t serial;              /* 串口设备句柄 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 配置参数 */


/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;




/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
    rt_sem_release(&rx_sem);

    return RT_EOK;
}

static void Re_music(void *args)
{
    static char buffer1[4]={0xAA,0x05,0x00,0xAF};
    rt_uint32_t tx_length;
    tx_length=4;
    int i = 0;
    while(i < 4){
    rt_device_write(serial,0,&buffer1[i],tx_length);
    i++;}
    rt_kprintf("上一条\n");
}
static void next_music(void *args)
{
    static char buffer1[4]={0xAA,0x06,0x00,0xB0};
    rt_uint32_t tx_length;
    tx_length=4;
    int i = 0;
    while(i < 4){
    rt_device_write(serial,0,&buffer1[i],tx_length);
    i++;}
    rt_kprintf("下一条\n");

}

static void serial_thread_entry(void *parameter)
{

  //  char ch;
    int jun = 1;
    while (1)
      
    {		
             /*发送缓冲区是固定为5个字节长度的*/
        static char uart2_tx_buffer[5]={0xAA,0x02,0x00,0xAC,0x05};
        rt_uint32_t tx_length;
        tx_length=5;
    rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, Re_music, RT_NULL);
    rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE);


    rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, next_music, RT_NULL);
    rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
        if(jun){
        rt_device_write(serial,0,&uart2_tx_buffer[0],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[1],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[2],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[3],tx_length);
        rt_kprintf("测试成功!");}
        /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */					
             jun = 0;
    }
}

static int mdr1(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];

    if (argc == 2)
    {
        rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
    }

    /* 查找系统中的串口设备 */
    serial = rt_device_find(uart_name);

    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    config.baud_rate = BAUD_RATE_9600;    //配置串口通信为9600
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.parity = PARITY_NONE;
    /* 打开设备后才可修改串口配置参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);
    /* 发送字符串 */
//    rt_device_write(serial, 0, str, (sizeof(str) - 1));

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(mdr1, uart JUN);

可以自行添加到RT-thread的BSP中,并烧录到开发板中。