简体   繁体   English

嵌入式系统:MSP430g2553 IAR 编程端口/引脚 BASIC 输入/Output 语法

[英]Embedded system: MSP430g2553 IAR programming ports/pins BASIC Input / Output syntax

To put it simply: How do I define and use ports / pins correctly in IAR EW with MSP430g2553?简而言之:如何在 IAR EW 中使用 MSP430g2553 正确定义和使用端口/引脚?

Ill use example to clarify what I do not understand.我会用例子来澄清我不明白的。

I have a simple state machine with 3 states.我有一个简单的 state 机器,有 3 个状态。 I want to program 3 pins for input and 2 pins for output.我想为 output 编程 3 个输入引脚和 2 个引脚。 Then, depending on inputs I manage state.然后,根据输入,我管理 state。

First, is this correct way of defining inputs / outputs?首先,这是定义输入/输出的正确方法吗?

 P1DIR |= BIT0 + BIT1; //pins 1.0 and 1.1 output
 P1DIR &= ~BIT2 + BIT3 + BIT4; // pins 1.2 , 1.3, 1.4 input

The above seems to me fairly straightforward to use, however, bigger question is how do I reference input pins in code?以上在我看来使用起来相当简单,但是,更大的问题是如何在代码中引用输入引脚? And how do I set output based on input?以及如何根据输入设置 output ?

To further my problem, here is my starting code for this state machine and I've put in pseudocode where I dont understand how to write syntax.为了进一步解决我的问题,这是我的 state 机器的起始代码,我在不了解如何编写语法的地方放入了伪代码。 Would be of great help if anyone could fill in the pseudocode and commentate a bit.如果有人可以填写伪代码并进行一些注释,那将有很大帮助。 I've looked many tutorials but I dont seem to get this simple thing from them.我看过很多教程,但我似乎没有从他们那里得到这个简单的东西。

# include "msp430g2553.h"
# include "stdio.h"
# include "math.h"

#define START 1
#define LEFT_ON 2
#define RIGHT_ON 3

char STATE;

main ()
{

 P1DIR |= BIT0 + BIT1; //port 1.0 and 1.1 output
 P1DIR &= ~BIT2 + BIT3 + BIT4; // port 1.2 , 1.3, 1.4 input
 
 WDTCTL = WDTPW + WDTHOLD;
 STATE =START;
 
 
 while(1)
 {
  //STATE = START;
   
   
   switch (STATE)
   {
   case START:
     {
     // Starting state I want both outputs to be set 1, I dont know how
       set p1.0 to 1
       set p1.1 to 1
       puts("START");

     //check inputs to switch state

      if (1.2 == 1 & 1.3==0 & 1.4==0) {
        STATE = RIGHT_ON;
      } else if (1.2 == 0 & 1.3==0 & 1.4==1)) {
        STATE = LEFT_ON;
      }  
     
     break;
     }
     
   case LEFT_ON:
     {
       // Here I wish to to put 1.0 output to 1 and 1.1 output to 0
          p1.0 set to 1
          p1.1 set to 0
       // now check if 1.3 is 1
          if (1.3 == 1) { 
             STATE = START;
             }     
       break;
     }
             
   case RIGHT_ON:
     {
      // Here I wish to to put 1.0 output to 0 and 1.1 output to 1
          p1.0 set to 0
          p1.1 set to 1
       // now check if 1.3 is 1
          if (1.3 == 1) { 
             STATE = START;
             }     
       break;
      }
   }//end of Switch  
 }// end of while
 
 
}// end of main

First, is this correct way of defining inputs / outputs?首先,这是定义输入/输出的正确方法吗?

I assume P1DIR is the correct data direction register (I don't know this particular MCU in detail), but apart from that: no, it isn't correct.我假设 P1DIR 是正确的数据方向寄存器(我不详细了解这个特定的 MCU),但除此之外:不,它不正确。 First of all, use bitwise OR |首先,使用按位 OR | not addition.不是加法。 They give the same result but + makes the code look strange and a bit harder to read.它们给出了相同的结果,但是+使代码看起来很奇怪并且有点难以阅读。 The average C programming book will tell you to do:一般的C编程书会告诉你怎么做:

P1DIR |= BIT0 | BIT1;

Note that P1DIR |=... will leave all pins currently set as output as they are.请注意, P1DIR |=...将保留当前设置为 output 的所有引脚。 That may or may not be what you want.这可能是也可能不是你想要的。

To set a port pin active then simply do the same, SOME_PORT_REGISTER |= PIN_MASK;要设置端口引脚处于活动状态,只需执行相同操作, SOME_PORT_REGISTER |= PIN_MASK; . . Similarly, you can toggle a single pin with ^= which is bitwise XOR.同样,您可以使用按位异或的^=切换单个引脚。 Or set it to zero with &= ~(mask) .或者使用&= ~(mask)将其设置为零。

P1DIR &= ~BIT2 + BIT3 + BIT4; is wrong, ~ is a unary operator that only applies to one operand.错了, ~是一元运算符,只适用于一个操作数。 Corrected code:更正的代码:

P1DIR &= ~(BIT2 | BIT3 | BIT4);

I've looked many tutorials看了很多教程

Most tutorials on the web are unfortunately quite bad.不幸的是,web 的大多数教程都很糟糕。 Start by reading a decent C programming book before anything else.首先阅读一本像样的 C 编程书,然后再阅读其他内容。 Once you've learnt the basics of C, you can go look for tutorials.一旦您了解了 C 的基础知识,您就可以 go 寻找教程。

For example this article about register access I wrote here, it assumed that the reader already knows C: How to access a hardware register from firmware?例如我在这里写的这篇关于寄存器访问的文章,假设读者已经知道 C:如何从固件访问硬件寄存器?

As for full beginner tutorials, I think this one has better quality than most: Embedded Software in C for an ARM Cortex M, Jonathan W. Valvano and Ramesh Yerraballi .至于完整的初学者教程,我认为这个比大多数教程质量更好: C 中的嵌入式软件,用于 ARM Cortex M、Jonathan W. Valvano 和 Ramesh Yerraballi (It's Cortex M not MSP430 but the principles are very similar no matter MCU. The same author also has an older tutorial that used NXP HSC12 examples, which is another 16 bitter even more similar to MSP430.) (它是 Cortex M 不是 MSP430 但无论 MCU 原理都非常相似。同一作者还有一个使用 NXP HSC12 示例的旧教程,这是另一个 16 苦涩,甚至更类似于 MSP430。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM