STM32单片机照相机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "usmart.h"
- #include "malloc.h"
- #include "sdio_sdcard.h"
- #include "w25qxx.h"
- #include "ff.h"
- #include "exfuns.h"
- #include "text.h"
- #include "piclib.h"
- #include "string.h"
- #include "math.h"
- #include "ov7670.h"
- #include "beep.h"
- #include "timer.h"
- #include "exti.h"
- #include "ov7725.h"
-
- #define OV7725 1
- #define OV7670 2
- extern u8 ov_sta; //在exit.c里面定义
- extern u8 ov_frame; //在timer.c里面定义
- //更新LCD显示(OV7725)
- void OV7725_camera_refresh(void)
- {
- u32 i,j;
- u16 color;
- if(ov_sta)//有帧中断更新
- {
- LCD_Scan_Dir(U2D_L2R);//从上到下,从左到右
- LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
- if(lcddev.id==0X1963)
- LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,240,320);//将显示区域设置到屏幕中央
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- OV7725_RRST=0; //开始复位读指针
- OV7725_RCK_L;
- OV7725_RCK_H;
- OV7725_RCK_L;
- OV7725_RRST=1; //复位读指针结束
- OV7725_RCK_H;
- for(i=0;i<240;i++)
- {
- for(j=0;j<320;j++)
- {
- OV7725_RCK_L;
- color=GPIOC->IDR&0XFF; //读数据
- OV7725_RCK_H;
- color<<=8;
- OV7725_RCK_L;
- color|=GPIOC->IDR&0XFF; //读数据
- OV7725_RCK_H;
- LCD->LCD_RAM=color;
- }
- }
- ov_sta=0; //清零帧中断标记
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- }
- }
- //更新LCD显示(OV7670)
- void OV7670_camera_refresh(void)
- {
- u32 j;
- u16 color;
- if(ov_sta)//有帧中断更新
- {
- LCD_Scan_Dir(U2D_L2R);//从上到下,从左到右
- if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//将显示区域设置到屏幕中央
- else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- OV7670_RRST=0; //开始复位读指针
- OV7670_RCK_L;
- OV7670_RCK_H;
- OV7670_RCK_L;
- OV7670_RRST=1; //复位读指针结束
- OV7670_RCK_H;
- for(j=0;j<76800;j++)
- {
- OV7670_RCK_L;
- color=GPIOC->IDR&0XFF; //读数据
- OV7670_RCK_H;
- color<<=8;
- OV7670_RCK_L;
- color|=GPIOC->IDR&0XFF; //读数据
- OV7670_RCK_H;
- LCD->LCD_RAM=color;
- }
- ov_sta=0; //清零帧中断标记
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- }
- }
-
- //文件名自增(避免覆盖)
- //组合成:形如"0:PHOTO/PIC13141.bmp"的文件名
- void camera_new_pathname(u8 *pname)
- {
- u8 res;
- u16 index=0;
- while(index<0XFFFF)
- {
- sprintf((char*)pname,"0:PHOTO/PIC%05d.bmp",index);
- res=f_open(ftemp,(const TCHAR*)pname,FA_READ);//尝试打开这个文件
- if(res==FR_NO_FILE)break; //该文件名不存在=正是我们需要的.
- index++;
- }
- }
- int main(void)
- {
- u8 sensor=0;
- u8 res;
- u8 *pname; //带路径的文件名
- u8 key; //键值
- u8 i;
- u8 sd_ok=1; //0,sd卡不正常;1,SD卡正常.
-
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- usmart_dev.init(72); //初始化USMART
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //初始化按键
- LCD_Init(); //初始化LCD
- BEEP_Init(); //蜂鸣器初始化
- W25QXX_Init(); //初始化W25Q128
- my_mem_init(SRAMIN); //初始化内部内存池
- exfuns_init(); //为fatfs相关变量申请内存
- f_mount(fs[0],"0:",1); //挂载SD卡
- f_mount(fs[1],"1:",1); //挂载FLASH.
- POINT_COLOR=RED;
- while(font_init()) //检查字库
- {
- LCD_ShowString(30,50,200,16,16,"Font Error!");
- delay_ms(200);
- LCD_Fill(30,50,240,66,WHITE);//清除显示
- }
- Show_Str(30,50,200,16,"战舰STM32F1开发板",16,0);
- Show_Str(30,70,200,16,"照相机实验",16,0);
- Show_Str(30,90,200,16,"KEY0:拍照",16,0);
- Show_Str(30,110,200,16,"正点原子@ALIENTEK",16,0);
- Show_Str(30,130,200,16,"2015年1月20日",16,0);
- res=f_mkdir("0:/PHOTO"); //创建PHOTO文件夹
- if(res!=FR_EXIST&&res!=FR_OK) //发生了错误
- {
- Show_Str(30,150,240,16,"SD卡错误!",16,0);
- delay_ms(200);
- Show_Str(30,170,240,16,"拍照功能将不可用!",16,0);
- sd_ok=0;
- }else
- {
- Show_Str(30,150,240,16,"SD卡正常!",16,0);
- delay_ms(200);
- Show_Str(30,170,240,16,"KEY0:拍照",16,0);
- sd_ok=1;
- }
- pname=mymalloc(SRAMIN,30); //为带路径的文件名分配30个字节的内存
- while(pname==NULL) //内存分配出错
- {
- Show_Str(30,190,240,16,"内存分配失败!",16,0);
- delay_ms(200);
- LCD_Fill(30,190,240,146,WHITE);//清除显示
- delay_ms(200);
- }
- while(1)//初始化OV7725_OV7670
- {
- if(OV7725_Init()==0)
- {
- sensor=OV7725;
- LCD_ShowString(30,190,200,16,16,"OV7725 Init OK ");
- while(1)
- {
- key=KEY_Scan(0);
- if(key==KEY0_PRES)
- {
- OV7725_Window_Set(320,240,0);//QVGA模式输出
- break;
- }
- else if(key==KEY1_PRES)
- {
- OV7725_Window_Set(320,240,1);//VGA模式输出
- break;
- }
- i++;
- if(i==100)LCD_ShowString(30,206,200,16,16,"KEY0:QVGA KEY1:VGA"); //闪烁显示提示信息
- if(i==200)
- {
- LCD_Fill(30,206,200,250+16,WHITE);
- i=0;
- }
- delay_ms(5);
- }
- OV7725_CS=0;
- break;
- }
- else if(OV7670_Init()==0)
- {
- sensor=OV7670;
- LCD_ShowString(30,190,200,16,16,"OV7670 Init OK ");
- delay_ms(1500);
- OV7670_Window_Set(12,176,240,320);//设置窗口
- OV7670_CS=0;
- break;
- }
- else
- {
- LCD_ShowString(30,190,200,16,16,"OV7725_OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(30,190,239,246,WHITE);
- delay_ms(200);
- }
- }
- TIM6_Int_Init(10000,7199); //10Khz计数频率,1秒钟中断
- EXTI8_Init(); //使能外部中断8,捕获帧中断
- LCD_Clear(BLACK);
- while(1)
- {
- key=KEY_Scan(0);//不支持连按
- if(key==KEY0_PRES)
- {
- if(sd_ok)
- {
- LED1=0; //点亮DS1,提示正在拍照
- camera_new_pathname(pname);//得到文件名
- if(bmp_encode(pname,(lcddev.width-240)/2,(lcddev.height-320)/2,240,320,0))//拍照有误
- {
- Show_Str(40,130,240,12,"写入文件错误!",12,0);
- }else
- {
- Show_Str(40,130,240,12,"拍照成功!",12,0);
- Show_Str(40,150,240,12,"保存为:",12,0);
- Show_Str(40+42,150,240,12,pname,12,0);
- BEEP=1; //蜂鸣器短叫,提示拍照完成
- delay_ms(100);
- }
- }else //提示SD卡错误
- {
- Show_Str(40,130,240,12,"SD卡错误!",12,0);
- Show_Str(40,150,240,12,"拍照功能不可用!",12,0);
- }
- BEEP=0;//关闭蜂鸣器
- LED1=1;//关闭DS1
- delay_ms(1800);//等待1.8秒钟
- LCD_Clear(BLACK);
- }else delay_ms(5);
-
- if(sensor==OV7725)
- OV7725_camera_refresh();//更新显示
- else if(sensor==OV7670)
- OV7670_camera_refresh();//更新显示
-
- i++;
- if(i>=40)//DS0闪烁.
- {
- i=0;
- LED0=!LED0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
照相机实验.rar
(1.66 MB, 下载次数: 102)
|