数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 3976|回复: 20

[other] 试试让百度的AI帮我们写单片机程序

[复制链接]
发表于 2023-8-31 15:41:18 | 显示全部楼层 |阅读模式
本帖最后由 猪小呆 于 2023-8-31 15:57 编辑

百度文心一言今天免费对社会开放了,之前看坛友做内阻仪里面提到了一个DFT算法计算内阻,不懂DFT是啥也不会写程序,就让百度帮忙写试试,它还真能写出来,以后不会写单片机程序就让百度AI帮我们写就可以了,大家看看它写的怎么样?
https://yiyan.baidu.com/




这是用STM32编写的代码:
  1. ; DFT program for STM32 microcontroller  
  2.   
  3. ; Define memory banks  
  4. bank1 .word 0x20000000  
  5. bank2 .word 0x20002000  
  6. bank3 .word 0x20004000  
  7. bank4 .word 0x20006000  
  8.   
  9. ; Define variables  
  10. input1 bank1, 8 ; input data, 8 words  
  11. input2 bank2, 8 ; input data, 8 words  
  12. output bank3, 16 ; output data, 16 words  
  13. temp bank4, 16 ; temporary storage, 16 words  
  14.   
  15. ; Define constants  
  16. TWID = $3FF ; twiddle factor table index  
  17.   
  18. ; Define functions  
  19. DFT:  
  20.     push {r4-r7, lr} ; save registers  
  21.     mov r4, r0 ; copy address of input1 to r4  
  22.     mov r5, r1 ; copy address of input2 to r5  
  23.     mov r6, r2 ; copy address of output to r6  
  24.     mov r7, #8 ; set loop counter to 8  
  25.     mov lr, pc ; jump to loop address  
  26. loop:  
  27.     push {r4-r6, lr} ; save registers  
  28.     mov r4, r0 ; copy address of input1 to r4  
  29.     mov r5, r1 ; copy address of input2 to r5  
  30.     mov r6, r2 ; copy address of output to r6  
  31.     mov r7, #16 ; set loop counter to 16  
  32.     mov lr, pc ; jump to inner loop address  
  33. inner_loop:  
  34.     push {r4-r7} ; save registers  
  35.     ldr r4, [r4, #0] ; load input1 word  
  36.     ldr r5, [r5, #0] ; load input2 word  
  37.     mul r4, r5 ; multiply input1 and input2  
  38.     ldr r5, =TWID * 4 ; load address of twiddle factor table  
  39.     add r5, r6, r5, lsl #2 ; add output address to twiddle table address  
  40.     ldr r5, [r5] ; load twiddle factor from table  
  41.     mls r4, r5, r4, r4 ; multiply and subtract  
  42.     ldr r5, =temp ; load address of temp storage  
  43.     str r4, [r5, #0] ; store result in temp word 1  
  44.     ldr r4, [r4], #4 ; load input1 word (next)  
  45.     str r4, [r5, #4] ; store result in temp word 2  
  46.     subs r7, #1 ; decrement loop counter  
  47.     bne inner_loop ; repeat inner loop if not done (loop counter != 0)  
  48.     pop {r4-r7} ; restore registers  
  49.     strh r0, [r6], #2 ; store input1 word in output (next)  
  50.     strh r1, [r6], #2 ; store input2 word in output (next)  
  51.     subs lr, #1 ; decrement outer loop counter  
  52.     bne loop ; repeat outer loop if not done (loop counter != 0)  
  53.     pop {r4-r6, lr} ; restore registers and return address  
  54.     pop {r4-r7, lr} ; restore saved registers and return address  
  55.     bx lr ; return to caller  
  56. twiddle_loop:  
  57.     push {lr} ; save return address  
  58.     bl DFT ; call DFT function with twiddle factors as inputs and outputs
复制代码



这是用STM32 C语言写的:
  1. #include "stm32f10x.h"  
  2. #include "arm_math.h"  
  3.   
  4. #define PI 3.14159265358979323846  
  5.   
  6. int main(void)  
  7. {  
  8.     arm_cfft_instance_f32 cfft_inst;  
  9.     arm_cfft_radix2_instance_f32 radix2_inst;  
  10.     arm_status status;  
  11.     float32_t input[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};  
  12.     float32_t output[16];  
  13.     float32_t twiddle[16] = {  
  14.         1.0, 0.999938962986879f, 0.9998889545483401f, 0.9998345776851702f,  
  15.         0.9997854545884765f, 0.9997385866311457f, 0.9996956057329524f,  
  16.         0.9996547117300371f, 0.9996162617181657f, 0.999580453731158f,  
  17.         0.999547428715867f, 0.9995168486287347f, 0.999488734243279f,  
  18.         0.9994634058435854f, 0.9994406618322378f};  
  19.   
  20.     // Initialize FFT instances  
  21.     status = arm_cfft_init_f32(&cfft_inst, 8);  
  22.     status = arm_cfft_radix2_init_f32(&radix2_inst, 8);  
  23.   
  24.     // Perform FFT on input data using CFFT and CFFTRI instances  
  25.     arm_cfft_f32(&cfft_inst, input, output);  
  26.     arm_cfft_radix2_f32(&radix2_inst, output);  
  27.   
  28.     // Multiply twiddle factors with output data  
  29.     for (int i = 0; i < 8; i++) {  
  30.         output[i] *= twiddle[i];  
  31.         output[i + 8] *= twiddle[i + 8];  
  32.     }  
  33.   
  34.     // Print the DFT results  
  35.     for (int i = 0; i < 16; i++) {  
  36.         printf("%f\n", output[i]);  
  37.     }  
复制代码
  1.   
  2.     while (1); // Infinite loop to keep program running  
  3. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
发表于 2023-8-31 16:02:49 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-31 16:04:48 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-31 16:15:48 | 显示全部楼层
游客请登录后查看回复内容

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
回复 支持 反对

使用道具 举报

发表于 2023-8-31 16:15:55 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 16:16:25 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 16:32:45 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 17:00:26 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 17:03:59 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 17:25:18 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 18:24:18 来自手机浏览器 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-8-31 18:26:39 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-9-1 12:46:49 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-9-1 12:49:53 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-1 18:46:18 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-1 18:49:18 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-9-1 18:54:33 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-1 18:56:04 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-9-2 08:50:51 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

发表于 2023-9-4 16:44:16 | 显示全部楼层
游客请登录后查看回复内容
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2026-4-2 12:17 , Processed in 0.265201 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz!

© MyDigit.Net Since 2006

快速回复 返回顶部 返回列表