记得我在读书的时候,也做过一个收音机,当时使用红米1S的手机包装盒做的成品如下图:
收音机模块采用的是RDA5807,主控芯片采用的是51单片机,配合OLED12864显示屏和红外接收头,实现了遥控搜台和频率显示。配合牛皮纸材质的手机盒,整体彰显出一种小清新的感觉。
毕业之后,我也一直把这个小收音机带在身边。一个人背井离乡,在陌生的城市搬了几次家后,一些重要的东西就逐渐丢失了。于是我准备再复刻一版这个收音机,算是对大学时光的怀念吧。
小米手机早已不再使用牛皮纸包装盒,想要100%复刻似乎不大可能。为了能使制作收音机的材料更容易获取,我准备采用PCB板做外壳,薅一把板厂的羊毛。其他小伙伴若有兴趣,也更容易仿制。
制作好的成品如下图:
硬件原理图
新设计采用了ESP8266作为主控芯片,显示屏使用的是240*240的LCD,WS2812用作信号指示灯。另配备了CD42充放电模块,断开充电线也能使用。详细的网络连接如下图:
8266引脚 | 所连接的模块 |
---|---|
0 | 编码器按键 |
2 | 编码器旋钮 A相 |
4 | RDA5807 -->SCL |
5 | RDA5807 -->SDA |
12 | WS2812_IN |
13 | LCD_SPI_MOSI |
14 | LCD_SPI_SCL |
15 | LCD_DC |
16 | 编码器旋钮 B相 |
硬件渲染图
软件代码
为了方便开发和修改,软件是采用MicroPython实现的,核心代码如下:
main.py
import time
from machine import Timer,Pin
from ST7789V import LCD_240_240
from biliflow import BiliFlow
from RDA58XX import RDA5807
from neopixel import NeoPixel
tim = Timer(-1)
bf = BiliFlow() #b站粉丝数量显示
lcd = LCD_240_240()#显示屏驱动
rda = RDA5807() #收音机启动
np = NeoPixel(Pin(12, Pin.OUT) , 1) #WS2812驱动
lcd.Disp_Pic(0x320000) #显示背景图片
def bilibili(t): #定时刷新B站的粉丝数
r, g, b = np[0]
np[0] = (50, g, b)
np.write()
lcd.Disp_Str(90,140,str(bf.get_follower()).encode('utf-8'))
np[0] = (0, g, b)
np.write()
tim.init(period=10000, mode=Timer.PERIODIC, callback=bilibili)
key0 = Pin(0, Pin.IN, Pin.PULL_UP)
key0_statue = key0.value()
key1 = Pin(2, Pin.IN, Pin.PULL_UP)
key1_statue = key1.value()
need_re = 0
while True:
if not key0_statue == key0.value(): #按下编码器 静音
key0_statue = key0.value()
time.sleep_ms(10)
if key0.value() == 0:
print("mute")
rda.mute()
if not key1_statue == key1.value(): #右旋编码器,向下搜台
key1_statue = key1.value()
if key1.value() == 0:
print("next")
rda.next_sta()
time.sleep_ms(3000)
need_re = 1
if need_re == 1: #刷新当前电台的频率
rda.refresh_info()
need_re = 0
lcd.Disp_Str(0,0,b' ')
lcd.Disp_Str(0,0,("FM:" + str(rda.tune)).encode('utf-8'))
print("FM:", rda.tune ,"RSSI:", rda.rssi)
另外还需要有收音机芯片驱动,LCD显示屏驱动及粉丝数显示相关的代码
RDA58XX.py
收音机驱动代码
from machine import Pin, SoftI2C
import time
import machine
class RDA5807(object):
def __init__(self):
self.i2c = SoftI2C(scl=Pin(4), sda=Pin(5), freq=10000)
self.i2c_buf = bytearray(2)
self.i2c_buf4 = bytearray(4)
self.is_mute = 0
self.rssi = 0
self.tune = 0
def next_sta(self): #向下搜台
self.i2c_buf[0] = 0xd3
self.i2c_buf[1] = 0x81
self.i2c.writeto(0x10, self.i2c_buf)
def pro_sta(self): #向上搜台
self.i2c_buf[0] = 0xd1
self.i2c_buf[1] = 0x81
self.i2c.writeto(0x10, self.i2c_buf)
def set_ch(self, ch): #设置频率eg. 965 unit 100KHz
self.i2c_buf4[0] = 0xd0
self.i2c_buf4[1] = 0x01
ch -= 870
self.i2c_buf4[2] = ((ch >> 2) & 0xff)
self.i2c_buf4[3] = ((ch << 5) & 0xff)
self.i2c.writeto(0x10, self.i2c_buf4)
def refresh_info(self): #获取当前频率,信号强度等
self.i2c.readfrom_into(0x11, self.i2c_buf4)
self.tune = 870 + self.i2c_buf4[1]
self.rssi = self.i2c_buf4[2] >> 1
def mute(self): #静音/取消静音
if self.is_mute == 1:
self.i2c_buf[0] = 0xd0
self.is_mute = 0
else:
self.i2c_buf[0] = 0x90
self.is_mute = 1
self.i2c_buf[1] = 0x01
self.i2c.writeto(0x10, self.i2c_buf)
ST7789V.py
显示屏驱动
from machine import Pin, SPI
import time
import machine
import esp
class LCD_240_240(object):
def __init__(self):
self.hspi = SPI(1, 40000000)
self.lcd_dc = Pin(15, Pin.OUT)
self.lcd_dc.off()
self.hspi.write(b'\x11')
time.sleep(0.05)
self.LCD_Cmd_Data(b'\x36',b'\x00')
self.LCD_Cmd_Data(b'\x3A',b'\x05')
self.LCD_Cmd_Data(b'\xB2',b'\x0C\x0C\x00\x33\x33')
self.LCD_Cmd_Data(b'\xB7',b'\x35')
self.LCD_Cmd_Data(b'\xBB',b'\x19')
self.LCD_Cmd_Data(b'\xC0',b'\x2C')
self.LCD_Cmd_Data(b'\xC2',b'\x01')
self.LCD_Cmd_Data(b'\xC3',b'\x12')
self.LCD_Cmd_Data(b'\xC4',b'\x20')
self.LCD_Cmd_Data(b'\xC6',b'\x0F')
self.LCD_Cmd_Data(b'\xD0',b'\xA4\xA1')
self.LCD_Cmd_Data(b'\xE0',b'\xD0\x04\x0D\x11\x13\x2B\x3F\x54\x4C\x18\x0D\x0B\x1F\x23')
self.LCD_Cmd_Data(b'\xE1',b'\xD0\x04\x0C\x11\x13\x2C\x3F\x44\x51\x2F\x1F\x1F\x20\x23')
self.lcd_dc.off()
self.hspi.write(b'\x21\x29')
def LCD_Cmd_Data(self,cmd, data):
self.lcd_dc.off()
self.hspi.write(cmd)
self.lcd_dc.on()
self.hspi.write(data)
def SetDisArea(self,xStart,yStart,xEnd,yEnd):
buf = bytearray(4)
buf[0] = 0
buf[1] = xStart
buf[2] = 0
buf[3] = xEnd
self.LCD_Cmd_Data(b'\x2A',buf)
buf[1] = yStart
buf[3] = yEnd
self.LCD_Cmd_Data(b'\x2B',buf)
self.lcd_dc.off()
self.hspi.write(b'\x2C')
def Disp_Color(self,Color): #整屏填充某个颜色
self.SetDisArea(0,0,239,239)
self.lcd_dc.on()
buf = bytearray(480)
for j in range(240):
buf[j*2] = Color >> 8
buf[j*2+1] = Color & 0xFF
for i in range(240):
self.hspi.write(buf)
def DrawPoint(self,x,y,color):#在指定位置打点
buf = bytearray(2)
buf[0] = 0
buf[1] = x
self.LCD_Cmd_Data(b'\x2A',buf)
buf[1] = y
self.LCD_Cmd_Data(b'\x2B',buf)
buf[0] = color >> 8
buf[1] = color &0xFF
self.LCD_Cmd_Data(b'\x2C',buf)
def Disp_Pic(self, star_addr): #显示图像,从Flash固定位置读取图像并显示在屏幕上
self.SetDisArea(0,0,239,239)
self.lcd_dc.on()
buf = bytearray(480)
byte_offset = star_addr
for i in range(240):
esp.flash_read(byte_offset, buf)
self.hspi.write(buf)
byte_offset += 480
def Disp_Str(self,x,y,str_datda):#显示字符串,需提前将字库烧录到0x300000位置
font_buf = bytearray(64)
for c in str_datda:
start_addr = 0x300000 + 65*(c - 0x20)
esp.flash_read(start_addr, font_buf)
self.SetDisArea(x,y,x+15,y+31)
x+=16
self.lcd_dc.on()
for j in range(64):
for k in range(8):
if (font_buf[j] & (0x01<<k)) == (0x01 << k):
self.hspi.write(b'\xf8\x00')
else:
self.hspi.write(b'\xff\xff')
bilibiliflow.py
获取B站粉丝数
import network
import json # 导入json功能模块
import urequests #导入urequests功能模块
class BiliFlow(object):
def __init__(self):
wifi = network.WLAN(network.STA_IF) # 将模块设为STA模式
wifi.active(True) # 开启WIFI
if not wifi.isconnected(): # 如果wifi模块未连接到热点
print('WiFi 连接中...')
wifi.connect('your_wifi' , 'xx123456') #连接自己的手机热点
while not wifi.isconnected(): #等待wifi连接
pass
print('WiFi 连接成功' , wifi.ifconfig()) #连接成功
def get_follower(self):#获取粉丝数,将下列vmid修改成自己的即可
r = urequests.get('http://api.bilibili.com/x/relation/stat?vmid=604244921')
p = json.loads(r.text)
return p['data']['follower']
结语
硬件设计资料全部在立创广场开源:https://oshwhub.com/Dr.Zhang/511radio
如果您喜欢的话,也可以尝试仿制一个哦,制作过程中遇到任何问题都可以在评论区留言。
B站搜索 我是鹏老师
,发现更多惊喜!