找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1535|回复: 2
打印 上一主题 下一主题
收起左侧

STC32G单片机编码器模式

[复制链接]
跳转到指定楼层
楼主
ID:25310 发表于 2022-6-20 17:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有没人测试过STC32G单片机的编码器模式。
1.例程 中 P1口选择P1.0 ,P1.2 ,STC32G 没有P1.2   。这么郁闷的事也有。
2.换到P2 口也没反应。
3.找到STC8H的编码器历程也不能用。

下面是STC8H编码器模式历程:
#include "reg51.h"
#include "intrins.h"

typedef struct TIM1_struct
{
    volatile unsigned char CR1;     /*!< control register 1 */
    volatile unsigned char CR2;     /*!< control register 2 */
    volatile unsigned char SMCR;    /*!< Synchro mode control register */
    volatile unsigned char ETR;     /*!< external trigger register */
    volatile unsigned char IER;     /*!< interrupt enable register*/
    volatile unsigned char SR1;     /*!< status register 1 */
    volatile unsigned char SR2;     /*!< status register 2 */
    volatile unsigned char EGR;     /*!< event generation register */
    volatile unsigned char CCMR1;   /*!< CC mode register 1 */
    volatile unsigned char CCMR2;   /*!< CC mode register 2 */
    volatile unsigned char CCMR3;   /*!< CC mode register 3 */
    volatile unsigned char CCMR4;   /*!< CC mode register 4 */
    volatile unsigned char CCER1;   /*!< CC enable register 1 */
    volatile unsigned char CCER2;   /*!< CC enable register 2 */
    volatile unsigned char CNTRH;   /*!< counter high */
    volatile unsigned char CNTRL;   /*!< counter low */
    volatile unsigned char PSCRH;   /*!< prescaler high */
    volatile unsigned char PSCRL;   /*!< prescaler low */
    volatile unsigned char ARRH;    /*!< auto-reload register high */
    volatile unsigned char ARRL;    /*!< auto-reload register low */
    volatile unsigned char RCR;     /*!< Repetition Counter register */
    volatile unsigned char CCR1H;   /*!< capture/compare register 1 high */
    volatile unsigned char CCR1L;   /*!< capture/compare register 1 low */
    volatile unsigned char CCR2H;   /*!< capture/compare register 2 high */
    volatile unsigned char CCR2L;   /*!< capture/compare register 2 low */
    volatile unsigned char CCR3H;   /*!< capture/compare register 3 high */
    volatile unsigned char CCR3L;   /*!< capture/compare register 3 low */
    volatile unsigned char CCR4H;   /*!< capture/compare register 3 high */
    volatile unsigned char CCR4L;   /*!< capture/compare register 3 low */
    volatile unsigned char BKR;     /*!< Break Register */
    volatile unsigned char DTR;     /*!< dead-time register */
    volatile unsigned char OISR;    /*!< Output idle register */
}TIM1_TypeDef;

#define TIM1_BaseAddress    0xFEC0

#define TIM1        ((TIM1_TypeDef xdata*)TIM1_BaseAddress)
#define PWMA_ENO    (*(unsigned char volatile xdata *)0xFEB1)
#define PWMA_PS     (*(unsigned char volatile xdata *)0xFEB2)

sfr     P0M0    =   0x94;
sfr     P0M1    =   0x93;
sfr     P1M0    =   0x92;
sfr     P1M1    =   0x91;
sfr     P_SW2   =   0xba;

sbit    P03     =   P0^3;

unsigned char cnt_H, cnt_L;

void main(void)
{
    P_SW2 = 0x80;

    P1M1 = 0x0f;
    P1M0 = 0x00;

    PWMA_ENO = 0x00;                            //配置成TRGI 的pin需关掉ENO对应bit并配成input
    PWMA_PS = 0x00;                             //00:PWM at P1

    TIM1-> PSCRH = 0x00;                        //预分频寄存器
    TIM1-> PSCRL = 0x00;

    TIM1-> CCMR1  = 0x21;                       //通道模式配置为输入,接编码器,滤波器4时钟
    TIM1-> CCMR2  = 0x21;                       //通道模式配置为输入,接编码器,滤波器4时钟

    TIM1-> SMCR   = 0x03;                       //编码器模式3

    TIM1-> CCER1  = 0x55;                       //配置通道使能和极性
    TIM1-> CCER2  = 0x55;                       //配置通道使能和极性

    TIM1-> IER  = 0x02;                         //使能中断

    TIM1-> CR1 |= 0x01;                         //使能计数器

    EA = 1;

    while (1);
}

/******************** PWM中断读编码器计数值**************************/
void PWMA_ISR() interrupt 26
{
    if (TIM1->SR1 & 0X02)
    {
        P03 = ~P03;
        cnt_H = TIM1->CCR1H;
        cnt_L = TIM1->CCR1L;
        TIM1->SR1 &= ~0X02;
    }
}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:514317 发表于 2022-6-22 11:31 | 只看该作者
STC32没试过   但STC8H使用P2口通过可以使用
回复

使用道具 举报

板凳
ID:409878 发表于 2022-6-23 08:36 | 只看该作者
感觉和51有点类似啊。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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