- #include "stm8s.h"
- #include "stm8s_gpio.h"
- #include "stm8s_exti.h"
- #include "stm8s.h"
- #include "stm8s_type.h"
- #include "stm8s.h"
- #include "stm8s_tim1.h"
- #include "stm8s_clk.h"
- #include "stm8s_conf.h"
- #define HC595_PORT (GPIOC)
- #define HC595_DIO (GPIO_PIN_3)
- #define HC595_SCK (GPIO_PIN_4)
- #define HC595_RCK (GPIO_PIN_5)
- void LS595_out(void);
- void LS595_in(u8 Data);
- /*
- #define BUTTON_PORT (GPIOC)
- #define BUTTOButtonStateN_PIN (GPIO_PIN_0)
- */
- #define AD_Conver (GPIOA)
- #define AD_DA (GPIO_PIN_1)
- #define AD_CK (GPIO_PIN_2)
- u8 table[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0xfe,0x9c,0xfc,0x9e,0x8e}; //段选
- u8 Wtable[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //位选
- void shumaguan_595(u8 wei,u8 duan) ;
- void display(void);
- u32 Sampling(void);
- u32 readconver(void);
- /* Private macro -------------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void Delay (u16 nCount);
- /* Private functions ---------------------------------------------------------*/
- /* Public variables ---------------------------------------------------------*/
- //u8 ButtonState = 0x00;
- //u8 A1, A2, A3, A4,A5,A6,A7,A8;
- u32 Lheight,AD;
- unsigned char A1, A2, A3, A4,A5,A6,A7,A8;
- void main(void)
- {
- u8 Leds; /* Contains which LEDs to operate */
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- /* Initialize I/Os in Output Mode */
- GPIO_Init(GPIOC,GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST); //推挽输出模式
- GPIO_Init(AD_Conver, AD_DA | AD_CK , GPIO_MODE_OUT_PP_LOW_FAST); //推挽输出模式
-
- /* Initialize I/O in Input Mode with Interrupt */
- // GPIO_Init(BUTTON_PORT, BUTTON_PIN, GPIO_MODE_IN_FL_IT);
- /* Initialize the Interrupt sensitivity */
- // EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
- // enableInterrupts();
- /* Toggles LEDs */
- while (1)
- {
-
- u32 c;
- // Lheight = ((Sampling()));
- Lheight=(Sampling()/1000-92)*75;
- //Lheight=1;
- //c=(readconver());
- // c= readconver();
- //Lheight = readconver()-93048;
- if(Lheight>0)
-
- //Lheight=Lheight;
- //Lheight = Sampling();
- {
- A8=Lheight%10;
- A7=(Lheight%100)/10;
- A6=(Lheight%1000)/100;
- A5=(Lheight%10000)/1000;
- A4=(Lheight%100000)/10000;
- A3=(Lheight%1000000)/100000;
- A2=(Lheight%10000000)/1000000;
- A1=Lheight/10000000;
- display();
- }
- else
- Lheight=0;
- A8=Lheight%10;
- A7=(Lheight%100)/10;
- A6=(Lheight%1000)/100;
- A5=(Lheight%10000)/1000;
- A4=(Lheight%100000)/10000;
- A3=(Lheight%1000000)/100000;
- A2=(Lheight%10000000)/1000000;
- A1=Lheight/10000000;
- display();
-
-
-
- }
- }
- void display(void)
- {
- u8 a;
- for(a=2000;a>0;a--);
- {
- shumaguan_595(Wtable[0],table[A1]);
- shumaguan_595(Wtable[1],table[A2]);
- shumaguan_595(Wtable[2],table[A3]);
- shumaguan_595(Wtable[3],table[A4]);
- shumaguan_595(Wtable[4],table[A5]);
- shumaguan_595(Wtable[5],table[A6]);
- shumaguan_595(Wtable[6],table[A7]);
- shumaguan_595(Wtable[7],table[A8]);
- shumaguan_595(0xff,table[0]);
- Delay(100);
- }
- }
-
- u32 readconver(void)
- {
- u8 i;
- u32 dat=0;
- //GPIO_WriteHigh(AD_Conver,AD_DA);
- //Delay(5);
- GPIO_WriteLow(AD_Conver,AD_CK); //当 AD_CK 为低电平时,芯片处于正常工作状态,使能AD
- while(GPIO_ReadInputPin(AD_Conver,AD_DA)!=0); //AD转换未结束则等待,否则开始读取
- GPIO_Init(AD_Conver, AD_DA , GPIO_MODE_IN_FL_NO_IT); //重新配置为输入模式
-
- for(i=0;i<24;i++) //读24个bit
- {
- GPIO_WriteHigh(AD_Conver,AD_CK);
- dat=dat<<1;//count=count<<1; //向左移一位
- GPIO_WriteLow(AD_Conver,AD_CK);
- // if(GPIO_ReadInputPin(AD_Conver,AD_DA)) 高电平返回非0,低返回0
- if(GPIO_ReadInputPin(AD_Conver,AD_DA)!=0)
-
- dat++; //if(AD_DA)count++; //当AD_DA为1时 count+1
- }
-
- GPIO_WriteHigh(AD_Conver,AD_CK);
- //dat=dat^0x800000; // 第25个脉冲下降沿来时,转换数据
- GPIO_WriteLow(AD_Conver,AD_CK);
- return(dat);
- }
- u32 Sampling(void)
- {
- u32 Sam[12],tmpmax,tmpmin,sum=0,Average;
- u8 i;
-
- for(i=0;i<12;i++)
- {
- Sam[i]=readconver();
- if(i==0)
- {
- tmpmax=Sam[0];
- tmpmin=Sam[0];
- }
- if(i>0)
- {
- if(Sam[i]>tmpmax)tmpmax=Sam[i];
- if(Sam[i]<tmpmin)tmpmin=Sam[i];
- }
- }
- for(i=0;i<12;i++)
- {
- if(!(Sam[i]==tmpmax||Sam[i]==tmpmin))
- {
- sum=sum+Sam[i];
- }
- }
- Average=sum/10;
- return(Average);
- }
- void shumaguan_595(u8 wei,u8 duan)
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- LS595_in (wei);
- LS595_in(duan);
- LS595_out();
- //TMR0_Delay1ms(1);
- }
-
- }
-
- void LS595_in(u8 Data)
- {
- u8 i;
-
- for(i=0;i<8;i++)
- {
- GPIO_WriteLow(HC595_PORT, HC595_SCK);
-
- if(Data&0x01)
- {
- GPIO_WriteHigh(HC595_PORT,HC595_DIO);
- }
- else
- {
- GPIO_WriteLow(HC595_PORT,HC595_DIO);
- }
- Data>>=1;
- GPIO_WriteHigh(HC595_PORT, HC595_SCK);
- }
- }
- /*写入数据函数*/
- void LS595_out(void)
- {
- GPIO_WriteLow(HC595_PORT, HC595_RCK);
-
- //RCK=1; //当RCK为上升沿时将数据并行输出
- Delay(1);
- GPIO_WriteHigh(HC595_PORT, HC595_RCK);
- //TMR0_Delay1ms(1);
- Delay(1);
- GPIO_WriteLow(HC595_PORT, HC595_RCK);
-
- }
- void Delay(u16 nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- #ifdef FULL_ASSERT
- void assert_failed(u8 *file, u16 line)
- #else
- void assert_failed(void)
- #endif
- {
- /* Add your own code to manage an assert error */
- /* Infinite loop */
- while (1)
- {
- }
- }
复制代码
使用HC595串行输入,并行输出
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
恩恩 我现在准备用中断写 ,while里面会出现数码管闪烁,看着不是蛮舒服
然后还用一个问题 003F3的flash太小,只有4K. 准备用寄存器写,用库函数占用的内存太大
一周热门 更多>