打印

每天帖个STM32F373例程,今天最简单的,指示灯闪。明天USART

[复制链接]
13736|25
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
uet_cache|  楼主 | 2012-12-4 18:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
每天此时帖个STM32F373例程,今天最简单的,指示灯闪。明天USART。上传到8个例程为止,其中会含AD,DA,I2C,TIM等,均会测试通过后再传。
例程为我们F373开发板上实验通过。本来想上传附件,可是含库文件压缩后的文件太大,所以只帖主要部分了。
开发板,使用STM32F373VCT6芯片,外部8M晶振,内部倍频后最大系统工作频率72M。仅供参考!帖个附件为时钟配置。
#include "stm32f37x.h"
main()
{
unsigned int i;
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  
  /* Configure PE14 and PE15 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

while(1)
{
  for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
  for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
  GPIO_SetBits(GPIOC,GPIO_Pin_13);
  for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
  for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
  GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
}
沙发
uet_cache|  楼主 | 2012-12-4 18:23 | 只看该作者
不好意思,附件传不上去。有要的人,在上面留言吧!晚点统一发给大家

使用特权

评论回复
板凳
figo20042005| | 2012-12-4 19:20 | 只看该作者
用的什么开发板??

使用特权

评论回复
地板
uet_cache|  楼主 | 2012-12-5 18:31 | 只看该作者
孩子这两天生病了,没心 写程序。USART还没调完。先上传TIM的吧。

//CPU:stm32f373vct6
//PB0ָʾµÆÿÃëÉÁ˸һ´Î,IO¿Ú°ëÃ뷴תһ´Î
//TIMER3 TIMEBASE
//By Cache.Lee 201212.5
#include "stm32f37x.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* LEDS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM_INIT(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 10000; //10ms¶¨Ê±Ê±¼ä
  TIM_TimeBaseStructure.TIM_Prescaler = 71;//1M ¼ÆÊýƵÂÊ ×¢Òâ´ËֵΪ16λ,²»Äܳ¬¹ý65535 ·ñÔò¼ÆÊý´íÎó
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
   
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

void TIM_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
main()
{
GPIO_Config();
TIM_Config();//´ËÓï¾äÒªÔÚTIM_INIT();֮ǰ ·ñÔòÎÞ·¨½øÖжÏ
TIM_INIT();

while(1);
}
void TIM3_IRQHandler(void)
{
static unsigned int count=0;

  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  count++;
  if(count>=50)//°ëÃ뷴תһ´Î
  {
    GPIOB->ODR ^= GPIO_Pin_0;
   count=0;
  }
}
}

使用特权

评论回复
5
uet_cache|  楼主 | 2012-12-5 18:35 | 只看该作者
好像有些中文好像看不到,以后我注意下,改成英文注释。
原程序MDK中,中文是可以看得到的。拷到TXT中也无法识别。就懒得再重新注释了。将就下吧,有问题的可以问!

使用特权

评论回复
6
wxlhonker| | 2012-12-6 09:35 | 只看该作者
顶一下,看好F3,可惜手头没板啊

使用特权

评论回复
7
song19881218| | 2012-12-6 10:14 | 只看该作者
正在找373的开发板,能否给个链接?

使用特权

评论回复
8
uet_cache|  楼主 | 2012-12-6 10:33 | 只看该作者
可以到我店铺看看!

使用特权

评论回复
9
uet_cache|  楼主 | 2012-12-6 12:35 | 只看该作者
孩子病好了。有心情写程序了。
呵呵,第3号程序,提前上传,USART的。明天上传2.2'tft LCD的。

//CPU:stm32f373vct6
//toggle PC13 while receive a new data from usart
//19200.8,1,NONE
//By Cache.Lee 2012.12.6

#include "stm32f37x.h"

void USART_Config(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
         NVIC_InitTypeDef NVIC_InitStructure;
  
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  
  GPIO_DeInit(GPIOA);
  
  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);   
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
  
  /* Configure pins as AF pushpull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* USARTx configured as follow:
  - BaudRate = 19200 baud  
  - Word Length = 8 Bits
  - Stop Bit = 1 Stop Bit
  - Parity = No Parity
  - Hardware flow control disabled (RTS and CTS signals)
  - Receive and transmit enabled
  */
       
       
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
       
  
  USART_DeInit(USART1);
  USART_InitStructure.USART_BaudRate = 19200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  
    /* USART enable */
       
  USART_Cmd(USART1, ENABLE);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}


main()
{
        unsigned int i;
        GPIO_InitTypeDef        GPIO_InitStructure;
       
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
       
        USART_Config();
        while(1)
        {
                for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
                for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);       
                for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);
                for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);for(i=0;i<50000;i++);       
                USART_SendData(USART1,0xaa); //
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
                USART_ClearFlag(USART1, USART_FLAG_TXE);
        }
}


void USART1_IRQHandler(void)
{
        u8 temp;
        if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)
   {
          USART_ClearITPendingBit(USART1, USART_IT_RXNE);
                    
          temp=USART_ReceiveData(USART1);
                 GPIOC->ODR ^= GPIO_Pin_13;
                 USART_SendData(USART1,temp); //transfer the received data back
     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
                 USART_ClearFlag(USART1, USART_FLAG_TXE);
   }
}

使用特权

评论回复
10
uet_cache|  楼主 | 2012-12-7 17:19 | 只看该作者
第四号程序:2.2‘TFTLCD显示,用到PB3,4,5. 其中有一个奇怪问题。PD12需要外接上位电阻,置1时才能将电平拉高,如果不接上位电阻,即使内部使能输出上拉,置1电平也只能达1.5V。PD7作普通IO,置内部上拉高电平正常。只有PD12不行。查了PD12也没有什么特殊功能。换个STM32F303VCT6芯片,内部上拉,只能将电平拉到1V。目前此原因还没找到。

main.c
//CPU:stm32f373vct6
//PB0ָʾµÆÿÃëÉÁ˸һ´Î,IO¿Ú°ëÃ뷴תһ´Î
//TIMER3 TIMEBASE
//2.2 TFT LCD(LTM022A69B)
//Display a string\circle\rectangle on the LCD.
//Designed By Cache.Lee 2012.12.5
#include "stm32f37x.h"
#include "LTM022A69B.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* LEDS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM_INIT(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 10000; //10ms¶¨Ê±Ê±¼ä
  TIM_TimeBaseStructure.TIM_Prescaler = 71;//1M ¼ÆÊýƵÂÊ ×¢Òâ´ËֵΪ16λ,²»Äܳ¬¹ý65535 ·ñÔò¼ÆÊý´íÎó
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
   
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

void TIM_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
main()
{
GPIO_Config();
TIM_Config();//´ËÓï¾äÒªÔÚTIM_INIT();֮ǰ ·ñÔòÎÞ·¨½øÖжÏ
TIM_INIT();

LCD_Init();
  LCD_Clear(Red);
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);
  LCD_DisplayStringLine(Line4,  "2.2 LCD tft   ");   
LCD_DrawLine(0,0,10,LCD_DIR_VERTICAL);
LCD_DrawRect(7,20,30,50);
LCD_DrawCircle(100,100,50);
while(1);
}
void TIM3_IRQHandler(void)
{
static unsigned int count=0;

  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  count++;
  if(count>=50)//°ëÃ뷴תһ´Î
  {
    GPIOB->ODR ^= GPIO_Pin_0;
   count=0;
  }
}
}


LTM022A69B.C文件
#include "LTM022A69B.h"
#include "fonts.c"
#include "stm32f37x.h"
const uint color[]={0xf800,0x07e0,0x001f,0xffe0,0x0000,0xffff,0x07ff,0xf81f};
static sFONT *LCD_Currentfonts;
static __IO uint16_t TextColor = 0x0000, BackColor = 0xFFFF;
//=============================================
void Delay(unsigned int t)
{
unsigned int n,m;
for(n=0;n<t;n++)
  for(m=0;m<600;m++);
}
void LCD_WR_REG(uchar index)
{
uchar i;
LCD_CS0;
LCD_RS0;
for(i=0;i<8;i++)
{
  LCD_SCL0;
  LCD_SCI0;
  LCD_SCL1;
}
for(i=0;i<8;i++)
{
  LCD_SCL0;
  if(index&0x80)
   LCD_SCI1;
  else
   LCD_SCI0;
  index=index<<1;
  LCD_SCL1;
}
LCD_CS1;
}
void LCD_WR_CMD(uchar index,uint val)
{
uchar i;
LCD_WR_REG(index);
LCD_RS1;
LCD_CS0;
for(i=0;i<16;i++)
{
  LCD_SCL0;
  if(val&0x8000)
     LCD_SCI1;
  else
    LCD_SCI0;

  LCD_SCL1;
  val=val<<1;
}
LCD_CS1;
}
void LCD_WR_Data(uint val)
{
uchar i;
for(i=0;i<16;i++)
{
  LCD_SCL0;
  if(val&0x8000) LCD_SCI1;
  else LCD_SCI0;
  val=val<<1;
  LCD_SCL1;
}
}
void LCD_SetFont(sFONT *fonts)
{
  LCD_Currentfonts = fonts;
}
void LCD_Init()
{
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD|RCC_AHBPeriph_GPIOB, ENABLE);

// GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_15);   
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_15);   
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_15);   
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_1);
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_4;
  GPIO_Init(GPIOB,&GPIO_InitStructure);   

  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_12;
   GPIO_Init(GPIOD,&GPIO_InitStructure);  


Delay(100);
LCD_RST0;
Delay(100);
LCD_RST1;
Delay(100);
//register reset
LCD_WR_CMD(0x0003,0x0001);
//oscillator start
LCD_WR_CMD(0x003A,0x0001);
Delay(10);
//y-setting
LCD_WR_CMD(0x0024,0x007B);
Delay(1);
LCD_WR_CMD(0x0025,0x003B);
LCD_WR_CMD(0x0026,0x0034);
    Delay(1);                  
LCD_WR_CMD(0x0027,0x0004);
LCD_WR_CMD(0x0052,0x0025);
Delay(1);
    Delay(1);
LCD_WR_CMD(0x0053,0x0033);
LCD_WR_CMD(0x0061,0x001C);
Delay(1);
LCD_WR_CMD(0x0062,0x002C);
LCD_WR_CMD(0x0063,0x0022);
Delay(1);
LCD_WR_CMD(0x0064,0x0027);
Delay(1);
LCD_WR_CMD(0x0065,0x0014);
Delay(1);
LCD_WR_CMD(0x0066,0x0010);

//Basical clock for 1 line (BASECOUNT[7:0]) number specified
LCD_WR_CMD(0x002E,0x002D);

//Power supply setting
LCD_WR_CMD(0x0019,0x0000);
Delay(20);
LCD_WR_CMD(0x001A,0x1000);
LCD_WR_CMD(0x001B,0x0023);
LCD_WR_CMD(0x001C,0x0C01);
LCD_WR_CMD(0x001D,0x0000);
LCD_WR_CMD(0x001E,0x0009);
LCD_WR_CMD(0x001F,0x0035);
LCD_WR_CMD(0x0020,0x0015);
LCD_WR_CMD(0x0018,0x1E7B);
//windows setting
LCD_WR_CMD(0x0008,0x0000);
LCD_WR_CMD(0x0009,0x00EF);
LCD_WR_CMD(0x000a,0x0000);
LCD_WR_CMD(0x000b,0x013F);
//LCD display area setting
LCD_WR_CMD(0x0029,0x0000);
LCD_WR_CMD(0x002A,0x0000);
LCD_WR_CMD(0x002B,0x00EF);
LCD_WR_CMD(0x002C,0x013F);
//Gate scan setting
LCD_WR_CMD(0x0032,0x0002);

//n line inversion line number
LCD_WR_CMD(0x0033,0x0000);
//Line inversion/frame inversion/interlace setting
LCD_WR_CMD(0x0037,0x0000);

//GOE1,GOE2 signal start
LCD_WR_CMD(0x003B,0x0001);

//Color mode
LCD_WR_CMD(0x0004,0x0000);
//windows mode setting
LCD_WR_CMD(0x0005,0x0014);

//Display setting register 2
LCD_WR_CMD(0x0001,0x0040);   
//display setting
LCD_WR_CMD(0x0000,0x0000);//display on

LCD_SetFont(&LCD_DEFAULT_FONT);
}
void LCD_test()
{
uint temp,num,i;
uchar n;
  
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);

LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(n=0;n<8;n++)
{
     temp=color[n];
    for(num=40*240;num>0;num--)
    LCD_WR_Data(temp);
}
for(n=0;n<8;n++)
{
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);
  
LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
     temp=color[n];
  for(i=0;i<240;i++)
  for(num=0;num<320;num++)
    LCD_WR_Data(temp);
}
LCD_CS1;
}
void LCD_Clear(uint p)
{
uint i,j;
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
  
LCD_WR_CMD(0x06,0x0000);
LCD_WR_CMD(0x07,0x0000);

LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(i=0;i<240;i++)
{
  for(j=0;j<320;j++)
  {
    LCD_WR_Data(p);//Delay(1);
  }
}
LCD_CS1;
}
void PutPixel(uint dx,uint dy)
{
LCD_WR_CMD(0x08,0x00);
LCD_WR_CMD(0x0a,0x0000);
LCD_WR_CMD(0x09,0xEF);
LCD_WR_CMD(0x0b,0x013F);
LCD_WR_CMD(0x06,dx);
LCD_WR_CMD(0x07,dy);
LCD_WR_CMD(0x0E,TextColor);
}
void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c)
{
uint32_t index = 0, i = 0,k;
   
LCD_WR_CMD(0x08,Xpos);  //x start point
LCD_WR_CMD(0x0a,Ypos);  //y start point
LCD_WR_CMD(0x09,Xpos+LCD_Currentfonts->Height-1); //x end point
LCD_WR_CMD(0x0b,Ypos+LCD_Currentfonts->Width-1); //y end point
LCD_WR_CMD(0x06,Xpos);
LCD_WR_CMD(0x07,Ypos);

LCD_WR_REG(0x0E);
LCD_CS0;
LCD_RS1;
for(index = 0; index < LCD_Currentfonts->Height; index++)
  {
   
    for(i = 0; i < LCD_Currentfonts->Width; i++)
    {
      k=i;//LCD_Currentfonts->Width-i;
      if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> k)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
        (((c[index] & (0x1 << k)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
      {
        LCD_WR_Data(BackColor);
      }
      else
      {
        LCD_WR_Data(TextColor);
      }
    }
  }
  LCD_CS1;  
}
sFONT *LCD_GetFont(void)
{
  return LCD_Currentfonts;
}
void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii)
{
  Ascii -= 32;
  LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
}
void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr)
{
  uint16_t refcolumn = 0;//LCD_PIXEL_WIDTH - 1;
  /* Send the string character by character on lCD */
  while ((*ptr != 0) & (((refcolumn + LCD_Currentfonts->Width) & 0xFFFF) >= LCD_Currentfonts->Width))
  {
    /* Display one character on LCD */
    LCD_DisplayChar(Line, refcolumn, *ptr);
    /* Decrement the column position by 16 */
    refcolumn += LCD_Currentfonts->Width;
    /* Point on the next character */
    ptr++;
  }
}
void LCD_SetBackColor(__IO uint16_t Color)
{
  BackColor = Color;
}
void LCD_SetTextColor(__IO uint16_t Color)
{
  TextColor = Color;
}
void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor)
{
  TextColor = _TextColor;
  BackColor = _BackColor;
}
void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor)
{
  *_TextColor = TextColor; *_BackColor = BackColor;
}
void LCD_ClearLine(uint8_t Line)
{
  uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
  /* Send the string character by character on lCD */
  while (((refcolumn + 1)& 0xFFFF) >= LCD_Currentfonts->Width)
  {
    /* Display one character on LCD */
    LCD_DisplayChar(Line, refcolumn, ' ');
    /* Decrement the column position by 16 */
    refcolumn -= LCD_Currentfonts->Width;
  }
}
void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos)
{
  LCD_WR_CMD(0x06,Xpos);
LCD_WR_CMD(0x07,Ypos);
}
void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
{
LCD_WR_CMD(0x08,Xpos);  //x start point
LCD_WR_CMD(0x0a,Ypos);  //y start point
LCD_WR_CMD(0x09,Xpos+LCD_Currentfonts->Height-1); //x end point
LCD_WR_CMD(0x0b,Ypos+LCD_Currentfonts->Width-1); //y end point
}
void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
{
  uint32_t i = 0;
  
  if(Direction == LCD_DIR_HORIZONTAL)
  {
  
    for(i = 0; i < Length; i++)
    {
   
      PutPixel(Xpos,Ypos+i);
    }
  
  }
  else
  {
    for(i = 0; i < Length; i++)
    {
   
   PutPixel(Xpos+i,Ypos);
    }
  }
}
void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
{
  LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
  
  LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
  LCD_DrawLine(Xpos, (Ypos + Width - 1), Height, LCD_DIR_VERTICAL);
}
void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius)
{
  int32_t  D;/* Decision Variable */
  uint32_t  CurX;/* Current X Value */
  uint32_t  CurY;/* Current Y Value */
  
//Ypos=320-Ypos;
  D = 3 - (Radius << 1);
  CurX = 0;
  CurY = Radius;
  
  while (CurX <= CurY)
  {
    PutPixel(Xpos + CurX, Ypos + CurY);
    PutPixel(Xpos + CurX, Ypos - CurY);
    PutPixel(Xpos - CurX, Ypos + CurY);
    PutPixel(Xpos - CurX, Ypos - CurY);
    PutPixel(Xpos + CurY, Ypos + CurX);
    PutPixel(Xpos + CurY, Ypos - CurX);
    PutPixel(Xpos - CurY, Ypos + CurX);
    PutPixel(Xpos - CurY, Ypos - CurX);
    if (D < 0)
    {
      D += (CurX << 2) + 6;
    }
    else
    {
      D += ((CurX - CurY) << 2) + 10;
      CurY--;
    }
    CurX++;
  }
}




LTM022A69B.H文件

#ifndef _LTM022A69B_H_
#define _LTM022A69B_H_
#include "stm32f37x.h"
#include "fonts.h"
#define uchar unsigned char
#define uint unsigned int
#define LCD_RST0   GPIO_ResetBits(GPIOD,GPIO_Pin_12)
#define LCD_RST1   GPIO_SetBits(GPIOD,GPIO_Pin_12)
#define LCD_CS1   GPIO_SetBits(GPIOD,GPIO_Pin_7)
#define LCD_CS0   GPIO_ResetBits(GPIOD,GPIO_Pin_7)
#define LCD_RS1  GPIO_SetBits(GPIOB,GPIO_Pin_4)
#define LCD_RS0  GPIO_ResetBits(GPIOB,GPIO_Pin_4)
#define LCD_SCL0 GPIO_ResetBits(GPIOB,GPIO_Pin_3)
#define LCD_SCL1 GPIO_SetBits(GPIOB,GPIO_Pin_3)
#define LCD_SCI0 GPIO_ResetBits(GPIOB,GPIO_Pin_5)
#define LCD_SCI1 GPIO_SetBits(GPIOB,GPIO_Pin_5)
#define LCD_COLOR_WHITE          0xFFFF
#define LCD_COLOR_BLACK          0x0000
#define LCD_COLOR_GREY           0xF7DE
#define LCD_COLOR_BLUE           0x001F
#define LCD_COLOR_BLUE2          0x051F
#define LCD_COLOR_RED            0xF800
#define LCD_COLOR_MAGENTA        0xF81F
#define LCD_COLOR_GREEN          0x07E0
#define LCD_COLOR_CYAN           0x7FFF
#define LCD_COLOR_YELLOW         0xFFE0
#define White               LCD_COLOR_WHITE
#define Black               LCD_COLOR_BLACK
#define Grey                LCD_COLOR_GREY
#define Blue                LCD_COLOR_BLUE
#define Blue2               LCD_COLOR_BLUE2
#define Red                 LCD_COLOR_RED
#define Magenta             LCD_COLOR_MAGENTA
#define Green               LCD_COLOR_GREEN
#define Cyan                LCD_COLOR_CYAN
#define Yellow              LCD_COLOR_YELLOW

#define LCD_LINE_0               LINE(0)
#define LCD_LINE_1               LINE(1)
#define LCD_LINE_2               LINE(2)
#define LCD_LINE_3               LINE(3)
#define LCD_LINE_4               LINE(4)
#define LCD_LINE_5               LINE(5)
#define LCD_LINE_6               LINE(6)
#define LCD_LINE_7               LINE(7)
#define LCD_LINE_8               LINE(8)
#define LCD_LINE_9               LINE(9)
#define LCD_LINE_10              LINE(10)
#define LCD_LINE_11              LINE(11)
#define LCD_LINE_12              LINE(12)
#define LCD_LINE_13              LINE(13)
#define LCD_LINE_14              LINE(14)
#define LCD_LINE_15              LINE(15)
#define LCD_LINE_16              LINE(16)
#define LCD_LINE_17              LINE(17)
#define LCD_LINE_18              LINE(18)
#define LCD_LINE_19              LINE(19)
#define LCD_LINE_20              LINE(20)
#define LCD_LINE_21              LINE(21)
#define LCD_LINE_22              LINE(22)
#define LCD_LINE_23              LINE(23)
#define LCD_LINE_24              LINE(24)
#define LCD_LINE_25              LINE(25)
#define LCD_LINE_26              LINE(26)
#define LCD_LINE_27              LINE(27)
#define LCD_LINE_28              LINE(28)
#define LCD_LINE_29              LINE(29)
#define Line0               LCD_LINE_0
#define Line1               LCD_LINE_1
#define Line2               LCD_LINE_2
#define Line3               LCD_LINE_3
#define Line4               LCD_LINE_4
#define Line5               LCD_LINE_5
#define Line6               LCD_LINE_6
#define Line7               LCD_LINE_7
#define Line8               LCD_LINE_8
#define Line9               LCD_LINE_9

#define LCD_PIXEL_WIDTH          0x0140
#define LCD_PIXEL_HEIGHT         0x00F0
#define LCD_DIR_HORIZONTAL       0x0000
#define LCD_DIR_VERTICAL         0x0001
#define LCD_DEFAULT_FONT         Font16x24
void Delay(unsigned int t);
void LCD_WR_REG(uchar index);
void LCD_WR_CMD(uchar index,uint val);
void LCD_WR_Data(uint val);
void LCD_SetFont(sFONT *fonts);
void LCD_Init(void);
void LCD_test(void);
void LCD_Clear(uint p);
void PutPixel(uint dx,uint dy);
void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c);
sFONT *LCD_GetFont(void);
void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii);
void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr);
void LCD_SetBackColor(__IO uint16_t Color);
void LCD_SetTextColor(__IO uint16_t Color);
void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor);
void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor);
void LCD_ClearLine(uint8_t Line);
void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos);
void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width);
void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction);
void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width);
void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius);
#endif



使用特权

评论回复
11
uet_cache|  楼主 | 2012-12-8 10:14 | 只看该作者
今天传个AD例程,AD连续转换单通道通过DMA存储。本例程测试通过。
其中有用到TFTLCD显示,昨天已上传液晶程序。今天只传主程序部分了。

//CPU:stm32f373vct6
//PB0指示灯每秒闪烁一次,IO口半秒反转一次
//TIMER3 TIMEBASE
//2.2 TFT LCD(LTM022A69B)
//Display AD Pointer Value on the LCD with PC0 as the Ad input.
//Designed By Cache.Lee 2012.12.5

#include "stm32f37x.h"
#include "LTM022A69B.h"

uint16_t RegularConvData[1];
#define ADC1_DR_Address             0x4001244C


void GPIO_Config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;
       
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* LEDS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}


void TIM_INIT(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
       
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 10000; //10ms定时时间
  TIM_TimeBaseStructure.TIM_Prescaler = 71;//1M 计数频率 注意此值为16位,不能超过65535 否则计数错误
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
   
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}
       
void TIM_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void ADC_Config(void)
{
  ADC_InitTypeDef     ADC_InitStructure;
  GPIO_InitTypeDef    GPIO_InitStructure;
  DMA_InitTypeDef     DMA_InitStructure;
  
  /* ADCCLK = PCLK2/4 */
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);   
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* DMA1 clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
  
  /* DMA1 Channel1 Config */
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr =(uint32_t)(RegularConvData);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  /* DMA1 Channel1 enable */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  /* ADC1 DeInit */  
  ADC_DeInit(ADC1);
  
  /* Enable ADC_DMA */
  ADC_DMACmd(ADC1, ENABLE);  
  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStructure);
  
  /* Configure the ADC1 in continuous mode */
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//AUTO continuous convert
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* Convert the ADC1 Channel 10 with 55.5 Cycles as sampling time */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);  
  ADC_Cmd(ADC1, ENABLE);  
  
  /* ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  
  while(ADC_GetResetCalibrationStatus(ADC1));
  
  /* ADC1 calibration start */
  ADC_StartCalibration(ADC1);
  
  while(ADC_GetCalibrationStatus(ADC1));
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
}


void LCD_ShowNum(uint8_t x,uint16_t y,uint16_t data)
{
LCD_DisplayChar(x,y,data/10000+48);
LCD_DisplayChar(x,y+25,data%10000/1000+48);   // %10000
LCD_DisplayChar(x,y+50,data%1000/100+48);
LCD_DisplayChar(x,y+75,data%100/10+48);         
LCD_DisplayChar(x,y+100,data%10+48);
}

main()
{       
        GPIO_Config();       
       
        LCD_Init();
  LCD_Clear(Red);       
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);       
  LCD_DisplayStringLine(Line2,  "AD TEST: ");  
        ADC_Config();       
        TIM_Config();//must set befor initing LCD, for the interrupt fuction will affect initing LCD .
        TIM_INIT(); //must be after TIM_Config()
        while(1);
}

void TIM3_IRQHandler(void)
{
        static unsigned int count=0;
       
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
                count++;
                if(count>=50)//半秒反转一次
                {
                  GPIOB->ODR ^= GPIO_Pin_0;
                        count=0;
                        //display the new pointer value on the lcd every half second
                        LCD_ShowNum(100,50,RegularConvData[0]);
                }
        }
}

使用特权

评论回复
12
uet_cache|  楼主 | 2012-12-9 11:24 | 只看该作者
STM32F373最大的特点在于它有3个16位AD转换器,9组差分输入信号,内部带可编程的增益放大器。昨天做了个SDADC例程,今天先上传给大家看看。
此例程使用PE7作AD输入端,开发板上接的PC0上,使用PC0上的电位器电压引到PE7口作可变电压,电压范围为0-3.3V。
此例程使用VDDA作参考电压。不多说了,帖出例程。
第六号程序:

//CPU:stm32f373vct6
//PB0指示灯每秒闪烁一次,IO口半秒反转一次
//TIMER3 TIMEBASE
//2.2 TFT LCD(LTM022A69B)
//SDADC PE7 SDADC_Channel_3 AS  AD INPUT (16BIT AD CHANNAL)
//GAIN IS 0.5,Vref=VDDA
//Designed By Cache.Lee 2012.12.8

#include "stm32f37x.h"
#include "LTM022A69B.h"
int16_t InjectedConvData = 0;
uint32_t TimingDelay = 0;

void GPIO_Config(void)
{
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* LEDS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void SysTick_Handler(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}
void DDelay(__IO uint32_t nTime)
{
  TimingDelay = nTime;
  while(TimingDelay != 0);
}

uint32_t SDADC1_Config(void)
{
  SDADC_InitTypeDef SDADC_InitStructure;
  SDADC_AINStructTypeDef SDADC_AINStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  uint32_t SDADCTimeout = 0;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDADC1, ENABLE);
  
  SDADC_DeInit(SDADC1);  
  /* PWR APB1 interface clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
  /* Enable SDADC analog interface */
  PWR_SDADCAnalogCmd(PWR_SDADCAnalog_1, ENABLE);
  
  /* Set the SDADC divider: The SDADC should run @6MHz */
  /* If Sysclk is 72MHz, SDADC divider should be 12 */
  RCC_SDADCCLKConfig(RCC_SDADCCLK_SYSCLK_Div12);
  /* GPIO Peripheral clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
  /* SDADC channel 3P pin configuration: PE7 */
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  /* Select External reference: The reference voltage selection is available
     only in SDADC1 and therefore to select the VREF for SDADC2/SDADC3, SDADC1
     clock must be already enabled */
  SDADC_VREFSelect(SDADC_VREF_VDDA);
  /* Insert delay equal to ~5 ms */
  DDelay(5);
  

  SDADC_Cmd(SDADC1, ENABLE);
  SDADC_InitModeCmd(SDADC1, ENABLE);
  SDADCTimeout = 30;
  /* wait for INITRDY flag to be set */
  while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_INITRDY) == RESET) && (--SDADCTimeout != 0));
  if(SDADCTimeout == 0)
  {
    /* INITRDY flag can not set */
    return 1;
  }
  
SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_SEOffset;
  SDADC_AINStructure.SDADC_Gain = SDADC_Gain_1_2;//GAIN is 0.5
  SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VSSA;
  SDADC_AINStructure.SDADC_Offset = 0;
  SDADC_AINInit(SDADC1, SDADC_Conf_0, &SDADC_AINStructure);
  SDADC_ChannelConfig(SDADC1, SDADC_Channel_3, SDADC_Conf_0);
  /* Channel3 configuration */
  SDADC_InitStructure.SDADC_Channel = SDADC_Channel_3;
  SDADC_InitStructure.SDADC_ContinuousConvMode = DISABLE;
  SDADC_InitStructure.SDADC_FastConversionMode = DISABLE;
  SDADC_Init(SDADC1, &SDADC_InitStructure);
  SDADC_InitModeCmd(SDADC1, DISABLE);
  /* configure calibration to be performed on conf0 */
  SDADC_CalibrationSequenceConfig(SDADC1, SDADC_CalibrationSequence_1);
  /* start PT100_SDADC Calibration */
  SDADC_StartCalibration(SDADC1);
  /* Set calibration timeout: 5.12 ms at 6 MHz in a single calibration sequence */
  SDADCTimeout = 4*30720 ;
  /* wait for PT100_SDADC Calibration process to end */
  while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_EOCAL) == RESET) && (--SDADCTimeout != 0));
  if(SDADCTimeout == 0)
  {
    /* EOCAL flag can not set */
    return 2;
  }
  /* SDADC successfully configured */
  return 0;
}
void LCD_ShowNum(uint8_t x,uint16_t y,uint16_t data)
{
LCD_DisplayChar(x,y,data/10000+48);
LCD_DisplayChar(x,y+25,data%10000/1000+48);   // %10000
LCD_DisplayChar(x,y+50,data%1000/100+48);
LCD_DisplayChar(x,y+75,data%100/10+48);  
LCD_DisplayChar(x,y+100,data%10+48);
}
main()
{
RCC_ClocksTypeDef RCC_Clocks;
  NVIC_InitTypeDef NVIC_InitStructure;

   /* SysTick end of count event each 1ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);

GPIO_Config();  
LCD_Init();
  LCD_Clear(Red);
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);
  LCD_DisplayStringLine(Line2,  "AD TEST(mV): ");  

if(SDADC1_Config() != 0)
  {
    LCD_DisplayStringLine(Line4, (uint8_t*)"Fail to configure   ");
    LCD_DisplayStringLine(Line5, (uint8_t*)"SigmaDelta Converter");
    while(1);
  }      
  

  SDADC_ITConfig(SDADC1, SDADC_IT_REOC, ENABLE);
  
  /* NVIC Configuration */
  NVIC_InitStructure.NVIC_IRQChannel = SDADC1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Enable software start conversion of the selected regular channel */
SDADC_SoftwareStartConv(SDADC1);
while(1);
}

void SDADC1_IRQHandler(void)
{
float t;
int InjectedConvData;

  if(SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_REOC) != RESET)
  {
    /* Get the converted value */
    InjectedConvData = SDADC_GetConversionValue(SDADC1);
   
  if(InjectedConvData<0)InjectedConvData=0;
  t=InjectedConvData ;
  t=t* 3300 /0.5/  65536;//GAIN is 0.5
  InjectedConvData=t;
  LCD_ShowNum(100,50,InjectedConvData);
   SDADC_SoftwareStartConv(SDADC1);
  }
}


使用特权

评论回复
13
uet_cache|  楼主 | 2012-12-9 11:25 | 只看该作者
明天上传第7号程序,DA。

使用特权

评论回复
14
uet_cache|  楼主 | 2012-12-10 08:49 | 只看该作者
今天一天准备做303VC的例程。为了不打扰调试程序,一早先把程序帖出来。
7号程序 DA:功能:将AD(12位 PC0)采集到的值通过DA(12位 PA5)输出。

//CPU:stm32f373vct6
//PB0ָʾµÆÿÃëÉÁ˸һ´Î,IO¿Ú°ëÃ뷴תһ´Î
//TIMER3 TIMEBASE
//2.2 TFT LCD(LTM022A69B)
//Display AD Pointer Value on the LCD with PC0 as the Ad input.
//out the DA value which is the AD value through DA CHANNEL2 PA5
//Designed By Cache.Lee 2012.12.5

#include "stm32f37x.h"
#include "LTM022A69B.h"
uint16_t RegularConvData[1];
#define ADC1_DR_Address             0x4001244C

void GPIO_Config(void)
{
GPIO_InitTypeDef        GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOE, ENABLE);
  
  /* LEDS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM_INIT(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 10000; //10ms¶¨Ê±Ê±¼ä
  TIM_TimeBaseStructure.TIM_Prescaler = 71;//1M ¼ÆÊýƵÂÊ ×¢Òâ´ËֵΪ16λ,²»Äܳ¬¹ý65535 ·ñÔò¼ÆÊý´íÎó
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
   
  /* TIM Interrupts enable */
  TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

void TIM_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void ADC_Config(void)
{
  ADC_InitTypeDef     ADC_InitStructure;
  GPIO_InitTypeDef    GPIO_InitStructure;
  DMA_InitTypeDef     DMA_InitStructure;
  
  /* ADCCLK = PCLK2/4 */
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);   
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* DMA1 clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
  
  /* DMA1 Channel1 Config */
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr =(uint32_t)(RegularConvData);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  /* DMA1 Channel1 enable */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  /* ADC1 DeInit */  
  ADC_DeInit(ADC1);
  
  /* Enable ADC_DMA */
  ADC_DMACmd(ADC1, ENABLE);  
  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStructure);
  
  /* Configure the ADC1 in continuous mode */
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//AUTO continuous convert
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* Convert the ADC1 Channel 10 with 55.5 Cycles as sampling time */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);  
  ADC_Cmd(ADC1, ENABLE);  
  
  /* ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  
  while(ADC_GetResetCalibrationStatus(ADC1));
  
  /* ADC1 calibration start */
  ADC_StartCalibration(ADC1);
  
  while(ADC_GetCalibrationStatus(ADC1));
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
}

void LCD_ShowNum(uint8_t x,uint16_t y,uint16_t data)
{
LCD_DisplayChar(x,y,data/10000+48);
LCD_DisplayChar(x,y+25,data%10000/1000+48);   // %10000
LCD_DisplayChar(x,y+50,data%1000/100+48);
LCD_DisplayChar(x,y+75,data%100/10+48);  
LCD_DisplayChar(x,y+100,data%10+48);
}

void DAC_Config(void)
{
  DAC_InitTypeDef    DAC_InitStructure;
  GPIO_InitTypeDef   GPIO_InitStructure;
  /* Enable GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* Configure PA.04 (DAC1_OUT2) in analog mode -------------------------*/
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Enable DAC clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC1, ENABLE);
  
  /* DAC1 channel1 Configuration */
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits2_0;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  DAC_Init(DAC1, DAC_Channel_2, &DAC_InitStructure);
  
  /* Enable DAC1 Channel1 */
  DAC_Cmd(DAC1, DAC_Channel_2, ENABLE);
}
main()
{
GPIO_Config();

LCD_Init();
  LCD_Clear(Red);
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);
  LCD_DisplayStringLine(Line2,  "AD TEST: ");  
ADC_Config();
DAC_Config();
TIM_Config();//must set befor initing LCD, for the interrupt fuction will affect initing LCD .
TIM_INIT(); //must be after TIM_Config()
while(1)
{
   /* Test DMA1 TC flag */
    while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET );
   
    /* Clear DMA TC flag */
    DMA_ClearFlag(DMA1_FLAG_TC1);
  DAC_SetChannel2Data(DAC1, DAC_Align_12b_R, RegularConvData[0]);
  //out the DA value which is the AD value
}
}
void TIM3_IRQHandler(void)
{
static unsigned int count=0;

  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  count++;
  if(count>=50)//°ëÃ뷴תһ´Î
  {
    GPIOB->ODR ^= GPIO_Pin_0;
   count=0;
   //display the new pointer value on the lcd every half second
   LCD_ShowNum(100,50,RegularConvData[0]);
  }
}
}

明天传最后一个.

使用特权

评论回复
15
uet_cache|  楼主 | 2012-12-11 16:00 | 只看该作者
The last one:

//CPU:stm32f303vct6
//2.2 TFT LCD(LTM022A69B)
//Designed By Cache.Lee 2012.12.5

#include "stm32f30x.h"
#include "LTM022A69B.h"
#include "eeprom.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Values of Variable1, Variable2 and Variable3 */
uint16_t VarValue1, VarValue2, VarValue3;

/* Virtual address defined by the user: 0xFFFF value is prohibited */
uint16_t VirtAddVarTab[NB_OF_VAR] = {0x5555, 0x6666, 0x7777};
uint16_t VarDataTab[NB_OF_VAR] = {0, 0, 0};

void LCD_ShowNum(uint8_t x,uint16_t y,uint16_t data)
{
LCD_DisplayChar(x,y,data/10000+48);
LCD_DisplayChar(x,y+25,data%10000/1000+48);   // %10000
LCD_DisplayChar(x,y+50,data%1000/100+48);
LCD_DisplayChar(x,y+75,data%100/10+48);         
LCD_DisplayChar(x,y+100,data%10+48);
}

main()
{       
  uint16_t varvalue = 0;
       
        LCD_Init();
  LCD_Clear(Red);       
  LCD_SetBackColor(Blue);
  LCD_SetTextColor(White);       
  LCD_DisplayStringLine(Line0,  " eeprom emulate: ");  
  LCD_DisplayStringLine(Line1,  "addr 0x5555");  
  LCD_DisplayStringLine(Line3,  "addr 0x6666");  
  LCD_DisplayStringLine(Line5,  "addr 0x7777");  
       
  
  /* Unlock the Flash Program Erase controller */
  FLASH_Unlock();

  /* EEPROM Init */
  EE_Init();
  
  /* Initialize variables to be used */
  VarValue1 = 0;
  VarValue2 = 0;
  VarValue3 = 0;
  
  /* Store successively many values of the three variables in the EEPROM */
  /* Store 100 values of Variable1, Variable2 and Variable3 in EEPROM */
  for (varvalue = 0; varvalue < 100; varvalue++)
  {
    VarValue1 += 1;
    VarValue2 += 2;
    VarValue3 += 3;
            
    EE_WriteVariable(VirtAddVarTab[0], VarValue1);
    EE_WriteVariable(VirtAddVarTab[1], VarValue2);
    EE_WriteVariable(VirtAddVarTab[2], VarValue3);
  }
  /* read the last stored variables data*/
  EE_ReadVariable(VirtAddVarTab[0], &VarDataTab[0]);
  EE_ReadVariable(VirtAddVarTab[1], &VarDataTab[1]);
  EE_ReadVariable(VirtAddVarTab[2], &VarDataTab[2]);
       
        LCD_ShowNum(48,190,VarDataTab[0]);
        LCD_ShowNum(95,190,VarDataTab[1]);
        LCD_ShowNum(140,190,VarDataTab[2]);
  
        while(1);
}


使用特权

评论回复
16
crazy87| | 2012-12-29 09:18 | 只看该作者
能用keil编写stm32f373程序吗?如何实现?

使用特权

评论回复
17
uet_cache|  楼主 | 2012-12-29 09:27 | 只看该作者
程序是在MDK下编写的。MDK4.6版本。

使用特权

评论回复
18
51921866| | 2012-12-29 17:05 | 只看该作者
标记一下,16位AD,以后用的着。

使用特权

评论回复
19
shanehuang| | 2013-2-4 15:31 | 只看该作者
楼主,SDADC有没遇到叠加误差,随着输入模拟值电压越大,误差越大,最大可以达到几十mv

使用特权

评论回复
20
lzq1804| | 2013-3-8 08:21 | 只看该作者
本帖最后由 lzq1804 于 2013-3-8 08:27 编辑

楼主能帖个STM32F373 USB与PC通讯的例程吗?
或者传点STM32F373 USB这方面的资料。
邮箱: 707543624@qq.com
时钟配置附件 我要一份

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:E精灵淘宝店   全系列STM32开发板 、电机评估套件 、实验箱 u-easytech.taobao.com QQ: 53755787 博客:http://blog.sina.com.cn/u/3193913184

13

主题

906

帖子

9

粉丝